2014-01-13 151 views
0

我在調試我編寫的代碼時遇到問題。它運行良好,直到某個點,然後停止並顯示:運行時錯誤1004應用程序定義或對象定義的錯誤Excel VBA:錯誤1004應用程序定義錯誤或對象定義錯誤

我很久沒寫VBA了,所以我的代碼可能是一團糟=)。

的問題似乎是:見註釋「生成UID爲當前進出口

'Sheet Definitions 
strSourceSheet = "MasterReport" 
strComponentSheet = "UniqueIdComponents" 

'defines row where data starts 
intEntryCount = 2 
intImpCount = 0 

'determine maximum number of rows for both sheets 
lngMaxRowSS = ThisWorkbook.Sheets(strSourceSheet).UsedRange.SpecialCells(xlCellTypeLastCell).Row 
lngMaxRowCS = ThisWorkbook.Sheets(strComponentSheet).UsedRange.SpecialCells(xlCellTypeLastCell).Row 

'Run until there are no more entries 
For intEntryCount = 2 To lngMaxRowSS 

    'Prevents to overwrite existing UIDs 
    If ThisWorkbook.Sheets(strSourceSheet).Range("BP" & intEntryCount) = "" Then 

    'Recieve next imperative on the Source List to find according UID Components 
    strSourceImperative = ThisWorkbook.Sheets(strSourceSheet).Range("A" & intEntryCount) 

     'Run until no new Imp UID is defind 
     For intImpCount = 11 To lngMaxRowCS 

      'Location of Imps on Component Sheet 
      strComponentImperative = ThisWorkbook.Sheets(strComponentSheet).Range("C" & intImpCount) 

      ' If the Source Imp = Component Imp then we create a UID for that Source IP 
      If strSourceImperative = strComponentImperative Then 

       'Assign Column to UID component in order to find the Column in the MasterReport 
       strUIDComponent1 = ThisWorkbook.Sheets(strComponentSheet).Range("D" & intImpCount) 
       strUIDComponent2 = ThisWorkbook.Sheets(strComponentSheet).Range("E" & intImpCount) 
       strUIDComponent3 = ThisWorkbook.Sheets(strComponentSheet).Range("F" & intImpCount) 
       strUIDComponent4 = ThisWorkbook.Sheets(strComponentSheet).Range("G" & intImpCount) 
       strUIDComponent5 = ThisWorkbook.Sheets(strComponentSheet).Range("H" & intImpCount) 
       strUIDComponent6 = ThisWorkbook.Sheets(strComponentSheet).Range("I" & intImpCount) 
       strUIDComponent7 = ThisWorkbook.Sheets(strComponentSheet).Range("J" & intImpCount) 

       'Generate UID for the Current Imp 
       strUID = ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent1 & intEntryCount) _ 
         & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent2 & intEntryCount) _ 
         & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent3 & intEntryCount) _ 
         & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent4 & intEntryCount) _ 
         & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent5 & intEntryCount) _ 
         & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent6 & intEntryCount) _ 
         & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent7 & intEntryCount) 

       'Writes UID into MasterReport 
       'ThisWorkbook.Sheets(strSourceSheet).Range("BP" & intEntryCount) = strUID 

       'Test Writes 
       ThisWorkbook.Sheets("Test").Range("A" & intEntryCount) = strUID 

      'If the Source Imp = Component Imp then we created a UID for that Source IP 
      End If 

     'If the two Source Imp <> Component Imp, go to next row on Component sheet and compare again 
     Next intImpCount 

    'Prevented to overwrite existing UIDs 
    End If 

Next intEntryCount 

的情況下我得到錯誤的組件是A,M,N,O,BK‘’ ,「」和入口計數是5718.它寫5718條目很好,然後顯示錯誤。

任何想法?

在此先感謝您的幫助!

+1

你已經張貼不作任何意義的代碼。也許你應該發佈更多的代碼,並描述你試圖用它實現的目標。 –

+0

以下是它的作用: strUID是由許多單元格值組成的唯一ID。這些值可能會有所不同,具體取決於用戶選擇多少個和哪些組件(列)。 由於組件的值存儲在單元格中,我使用strUIDComponent來告訴我哪個列和intEntryCount是哪一行。 我通過將多個單元格一起添加(請參閱代碼)來創建strUID,然後將其打印在不同的工作表中。 – Ergo

回答

2

ThisWorkbook.Sheets(strSourceSheet)出一個With聲明而代價值觀,你似乎是說這種說法是等價於:

With ThisWorkbook.Sheets(strSourceSheet) 

    strUID = .Range("A" & 5718) & _ 
      .Range("M" & 5718) _ 
      .Range("N" & 5718) _ 
      .Range("O" & 5718) _ 
      .Range("BK" & 5718) _ 
      .Range("" & 5718) _ 
      .Range("" & 5718) 

End With 

最後兩項是無效的範圍。

我不能對此進行測試的代碼,但像這樣的東西可能會滿足您的要求:

Dim WkShtComp As Worksheet 
    Dim WkShtSrc As Worksheet 
    Dim ColMast As String 

    With ThisWorkbook 
    Set WkShtComp = .Sheets(strComponentSheet) 
    Set WkShtSrc = .Sheets(strSourceSheet) 
    End With 

    strUID = "" 
    For Each ColMast in Array("D", "E", "F", "G", "H", "I", "J") 
    strUIDComponent = WKShtComp.Range(ColMast & intImpCount).Value 
    If strUIDComponent <> "" Then 
     strUID = strUID & WkShtSrc.Range(strUIDComponent & intEntryCount).Value 
    Endif 
    Next 
+0

你的代碼是它的樣子,是的。我也認爲「」可能是一個問題,但直到5718它工作,我有很多這樣的UID錯過了最後2個組件(這將是「」)。它寫得很好。 但由於它不是正確的代碼,我寧願修復它。任何想法如何我可以排除「」,而不必寫7獨立的IF語句? 我對VBA並不是很擅長,所以我的創意非常有限哈哈 – Ergo

+0

如果沒有更好的理解,你很難提出建議。我會考慮將'strUIDComponent'作爲一個數組,或者直接在循環中創建一個'strUID'而不使用'strUIDComponent'作爲中介。 –

+0

我已經添加了一些可能的但未經測試的代碼給我的答案。 –

相關問題