Excel VBA:错误1004应用程序定义或对象定义的错误

我无法debugging我写的代码。 它运行良好,直到某个点,然后停止并显示: 运行时错误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个条目就好了,然后显示错误。

有任何想法吗?

在此先感谢您的帮助!!

ThisWorkbook.Sheets(strSourceSheet)出来一个With语句并replace值,你似乎是说这个语句相当于:

 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 

最后两个条目不是有效的范围。

我无法testing这个代码,但是这样的东西可能会满足您的要求:

  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