Excel / VBA – 为每个循环和StringComp

我试图从一个外部/封闭的Excel表中获取一个列,并将其与一个打开的表中的列进行比较。

问题是,对于我的一些input,结果是错误的,因为比较是不正确的,所以程序告诉我,我的数组found()对于所有的数据都不够大

我的代码:

Private Sub CommandButton1_Click() Dim varData As Variant Dim objExcel As New Excel.Application Dim objSheet As Object Dim extRange As Variant Dim intRange As Variant Set intRange = ThisWorkbook.Sheets(1).Range("A4:A11") Dim loopStr As Variant Dim loopStr2 As Variant Dim found() As Variant Dim loopInt As Integer Dim endStr As Variant loopInt = 1 varData = Application.GetOpenFilename("Excel (*.xlsx), *.xlsx") If varData <> False Then objExcel.Workbooks.Open varDatei Set objSheets = objExcel.Sheets(1) objSheets.Activate LastRow = objSheets.Cells(Rows.Count, 3).End(xlUp).Row Set extRange = objSheets.Range("B3:B" & LastRow) ReDim found(1 To LastRow) For Each loopStr In extRange For Each loopStr2 In intRange If StrComp(loopStr, loopStr2) = True Then found(loopInt) = loopStr loopInt = loopInt + 1 End If Next loopStr2 Next loopStr loopStr = "" For Each loopStr In found endStr = endStr + " " + loopStr Next loopStr Debug.Print endStr Else MsgBox "Error" End If End Sub 

LastRow = objSheets.Cells(Rows.Count,3).End(xlUp).Row

“行”似乎没有定义给我。 通常的语法是

 LastRow = objSheets.Cells(objSheets.Rows.Count, 3).End(xlUp).Row 

(或者任何你想要计算行的表)或者

 LastRow = objSheets.Cells(.Rows.Count, 3).End(xlUp).Row 

为当前工作表。 检查什么debugging器说“Rows.Count”。 试试这个,如果你觉得有用,回复。