Vlookup语法和用户input问题

我试图创build一个比较两个用户input工作表的macros,然后根据不同的原因将差异移到不同的工作表。

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

然后它沿着一个单元格逐个单元格寻找第二个工作簿上的匹配序列(这主要是为了确保比较正确的数据的情况下,格式化是closures的)。 一旦find匹配的序列号,就比较第二个序列号是否符合条目,并取决于其中一个表格的不同或新的input。

我遇到的主要问题是VLookup。 它有多个错误424,1004和编译expression式错误。 我需要一点点指导,为什么它有这些问题。 我已经search了很多关于需要有括号来引用一个文件,但是当我完全按照这些格式它会引发expression式错误。

任何意见表示赞赏。

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的地方 。 你可以在它之前使用其余的代码。 基本上上面的代码:

  • 将源表分配给variables并将其直接传递给Vlookup参数。
  • 使用Vlookup通过VBA WorksheetFunction获取数据。

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

还要注意,最好是完全限定你正在工作的对象。
这是开始优化代码并避免运行时错误的好地方。