我正在尝试创建一个宏来比较两个用户输入的工作表,然后根据其不同的原因将差异移至不同的工作表。

该代码首先要求输入最新数据并打开该工作表。然后它会询问要比较的旧数据的位置,但不会打开它。它添加了要复制到的必要工作表。

然后,它逐列向下查找第二个工作簿上的匹配序列(这主要是为了确保在格式关闭的情况下比较正确的数据)。一旦找到匹配的序列号,它就会比较两个条目的第二个序列号,并根据其中一张表的输入是否不同或新输入来确定。

我遇到的主要问题是 VLookup。它有多个错误 424、1004 和编译表达式错误。我需要一些指导来解释为什么会出现这些问题。我已经搜索并发现了很多需要使用括号来引用文件的信息,但是当我完全遵循这些格式时,它会引发表达式错误。

任何建议表示赞赏。

Sub Compare()

'Open workbooks
''Worksheet 1

Dim filter As String
Dim caption As String
Dim WB1FN As String
Dim WB1 As Workbook

filter = "Excel Sheets (*.xlsx),*.xlsx"
caption = "Please select newest equipment file"
MsgBox (caption)
WB1FN = Application.GetOpenFilename(filter, , caption)

        If WB1FN = "False" Then
            MsgBox "File not selected to import"
            Exit Sub
        End If

Set WB1 = Application.Workbooks.Open(WB1FN)

''Worksheet 2

Dim caption2 As String
Dim WB2FN As String

filter = "Excel Sheets (*.xlsx),*.xlsx"
caption2 = "Please select previous equipment file"
MsgBox (caption2)
WB2FN = Application.GetOpenFilename(filter, , caption)

        If WB2FN = "False" Then
            MsgBox "File not selected to import"
            Exit Sub
        End If

'Comparing data
''MS find and compare

Dim MS1 As String
Dim ESN1 As String
Dim ESN2 As String
Dim LastRow As Long
Dim i As Integer
Dim d As Integer
Dim n As Integer
Dim Filename As String

d = 4
n = 4

Set WB1 = ActiveWorkbook

'Create sheets

Sheets.Add(After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)).Name = "A"
Sheets.Add(After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)).Name = "B"
Sheets.Add(After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)).Name = "C"

'Gets the last row number

ActiveWorkbook.Sheets(1).Activate
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

For i = 4 To LastRow

''Assigning MS1,ES1,ES2

    MS1 = Cells(i, 6)
    ESN1 = Cells(i, 15)
    ESN2 = Application.WorksheetFunction.VLookup(MS1, '[" & WB2FN & "]Sheet1'! [R3C6:R10000C15], 10, False)
''Compare ESN and copy data

        If ESN2 <> ESN1 Then
        cell.EntireRow.Copy Sheets(2).Cells(d, 1)
        n = d + 1
        ElseIf Application.WorksheetFunction.IsNA(ESN2) = "TRUE" Then
        cell.EntireRow.Copy Sheets(4).Cells(n, 1)
        n = n + 1
        End If
Next i

'X find and copy

Dim OEM As String

ActiveWorkbook.Sheets(2).Activate

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

n = 3
i = 3

For i = 3 To LastRow

''Check for X

    OEM = Cells(i, 4)

    If OEM = "x" Then
        cell.EntireRow.Copy Sheets(3).Cells(n, 1)
        n = n + 1
    End If

Next i

MsgBox "Compare successful"

End Sub
有帮助吗?

解决方案

have brackets to reference a file 仅当您将公式分配给单元格或区域时,才可以使用该方法。

例子:

Dim myformula As String

myformula = "=VLOOKUP(" & MS1 & _
    ",'[" & WB2FN & "]Sheet1'! [R3C6:R10000C15], 10, False)"
Range("A1").Formula = myformula

但如果你使用 VBA 工作表函数, ,您需要以某种方式访问​​运行时从中获取数据的数据库或表。这意味着您必须在参数上传递对象,而不是像上面那样传递字符串。
就像是:

'~~> the rest of your code before Vlookup here

Dim wb As Workbook
Dim mytable As Range

Set wb = Workbooks.Open(WN2FN, , True) '~~> read only, avoid errors when file in use
Set mytable = wb.Sheets("Sheet1").Range("F3:O10000")

On Error Resume Next '~~> to handle when Vlookup returns #N/A or errors out
ESN2 = Application.WorksheetFunction.VLookup(MS1, mytable, 5, 0)
If Err.Number <> 0 Then myvalue = CVErr(xlErrNA)
On Error GoTo 0 '~~> reset error handling to trap other errors

Debug.Print ESN2

我刚刚提供了您使用的部分 Vlookup工作表函数. 。您可以使用之前的其余代码。基本上上面的代码:

  • 将源表分配给变量并将其直接传递给 查找 论据。
  • 用途 查找 通过 VBA工作表函数 获取数据。

记下 OERN(出错时继续下一步) 例程和 OEG0 (出错时转到 0).
在 VBA 中,当 工作表功能 返回错误(例如#N/A for Vlookup),代码出错并停止执行。没有 错误 就像我们在工作表公式中一样。因此,您需要使用错误处理例程来处理它。

另请注意,最好完全限定您正在处理的对象。
这是开始优化代码并避免运行时错误的好地方。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top