0

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

我的想法是从第二个来源获取订单总数,在第一个来源的订单总数之后将它们粘贴到工作表中,然后根据现有跟踪器中的订单号搜索新文件中的所有订单号。我有一个 for 循环,它应该从新文件中找到尚未在跟踪器中的订单号,然后插入包含该订单详细信息的行。我在检查字符串是否存在于数组中的 if 语句中收到类型不匹配错误。请看一下这段代码:

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

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

谢谢!

4

2 回答 2

2

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

做这个:

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

我认为这应该为您解决。

更新

您可能需要:

Dim ExistArray() As Variant

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

ExistArray = Application.Transpose(Array(Range("A1")))
于 2013-08-21T19:12:38.563 回答
0

根据需要从“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
于 2013-08-21T19:37:35.267 回答