search数组中的string时types不匹配错误

我正在研究一个将整合两个不同的订单数据源的macros。 第一个来源将包含旧订单以及一些新订单,第二个来源将只包含旧订单,并且将在手动更新的列中具有其他数据。

我的想法是从第二个来源获取订单总计,将它们粘贴到来自第一个来源的订单总计之后的工作表中,然后根据现有跟踪器的订单号从新文件中search所有订单编号。 我有一个for循环,应该从新文件中find尚未在跟踪器中的订单号,然后插入具有该订单明细的行。 我在if语句上收到types不匹配错误,该错误检查数组中是否存在该string。 请看这个代码:

Dim r As Integer For r = 1 To 1000 Dim NewOrd As String NewOrd = Range(Cells(r, 1), Cells(r, 1)).Value Dim ExistArray As Variant ExistArray = Range("a1", Range("a1").End(xlUp)) Sheets("Sheet2").Select If IsEmpty(NewOrd) Then Exit For End If If Not UBound(Filter(ExistArray, NewOrd)) >= 0 And NewOrd <> "" Then Rows(r).Select Selection.Copy Sheets("Sheet3").Select Rows(r).Select Selection.Insert Shift:=xlDown Application.CutCopyMode = False End If r = r + 1 Next r 

我尝试了几种不同的方法来设置数组,尝试添加显式选项,并试图嵌套for循环(不是我最明智的效率时刻)。 将不胜感激另一套眼睛!

谢谢!

将一个Range对象分配给一个数组总是会导致一个二维数组,这导致了错误。

做这个:

 ExistArray = Application.Transpose(Range("a1", Range("a1").End(xlUp))) 

我认为这应该为你解决。

更新

您可能需要:

 Dim ExistArray() As Variant 

您的范围对象也是有问题的,是一个单元格:

 ExistArray = Application.Transpose(Array(Range("A1"))) 

根据需要从“Sheet1”和“Sheet2”更改图纸名称:

 Sub tgr() Dim wsNew As Worksheet Dim wsTracker As Worksheet Dim rIndex As Long 'This is the sheet that contains the new data that needs to be added Set wsNew = Sheets("Sheet1") 'This sheet contains the old data Set wsTracker = Sheets("Sheet2") 'Go through each row in the new data For rIndex = 1 To wsNew.Cells(Rows.Count, "A").End(xlUp).Row 'Verify that the row isn't blank and that it doesn't already exist in wsTracker If Len(wsNew.Cells(rIndex, "A").Value) > 0 And WorksheetFunction.CountIf(wsTracker.Columns("A"), wsNew.Cells(rIndex, "A").Value) = 0 Then 'This is a new item that needs to be added 'Copy the row to the next available row in wsTracker wsNew.Rows(rIndex).Copy wsTracker.Cells(Rows.Count, "A").End(xlUp).Offset(1) End If Next rIndex Set wsNew = Nothing Set wsTracker = Nothing End Sub