Vlookup语法和用户输入问题

时间:2014-07-29 05:48:01

标签: 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

1 个答案:

答案 0 :(得分:0)

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.