excel - Vlookup 语法和用户输入问题

标签 excel vba syntax vlookup getopenfilename

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

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

然后它在第二个工作簿上逐个单元格地向下查找匹配的序列(这主要是为了确保它比较正确的数据以防万一格式关闭)。一旦找到匹配的序列号,它就会比较两个条目的第二个序列号,并取决于它是不同的还是新的输入到其中一张表中。

我遇到的主要问题是 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 WorksheetFunction 的部分。您可以在它之前使用其余代码。基本上上面的代码:
  • 将源表分配给变量并将其直接传递给 Vlookup 参数。
  • 通过 VBA WorksheetFunction 使用 Vlookup 来获取数据。

  • 记下 OERN(On Error Resume Next)例程和 OEG0(On Error Goto 0)。
    在 VBA 中,当工作表函数返回错误(例如 Vlookup 的 #N/A)时,代码会出错并停止执行。没有像我们在工作表公式中那样的 IFERROR。因此,您需要使用错误处理例程来处理它。

    另请注意,最好完全限定您正在处理的对象。
    This is a good place to start to optimize your codes and avoid runtime errors.

    关于excel - Vlookup 语法和用户输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25008940/

    相关文章:

    excel - 在 VBA 中显示 InputBox 对话框时如何滚动窗口?

    ms-access - 为什么 VBA Access 中 134.605*100 不等于 13460.5?

    excel - Workbooks.OpenText 无法正确解析 csv 文件 Excel 2016

    java - 备用java语言

    java - Java中的 "@Override"有什么用?

    php - 您的 SQL 语法有错误;检查手册以了解在 'AND ` user_id` = 1' 附近使用的正确语法

    excel - 定义一系列单元格。像 Shift + 单击

    excel - 如何像 Try/Catch 一样内联错误处理 block

    Excel VBA 打开文件夹并获取其中每个文件的 GPS 信息 (Exif) (2)

    vba - 用于创建新工作表的 Excel 宏