2014-01-13 121 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

我已经添加了一些可能的但未经测试的代码给我的答案。 –

相关问题