我有一个工作簿在另一个工作簿中执行Vlookup,我让它打开文件,然后运行Vlookup。出于某种原因,如果它首先打开文件,它只返回#N / A,但如果文件已经打开,它会发现值很好。这是代码中的东西吗?
Private Sub BarcodeVlookup(beginningNum As Integer, endingNum As Integer)
Dim pvsReport As Excel.Workbook
Dim tracker As Excel.Workbook
Dim lookFor As Range
Dim brandRng As Range
Dim i As Integer
Dim k As Integer
Dim r As Integer
Dim j As String
If Not IsFileOpen("\\msfs05\Data1\SHARE\MDCM_Reports\LowesCom\PVS\PVS_Report.xlsx") Then
Workbooks.Open ("\\msfs05\Data1\SHARE\MDCM_Reports\LowesCom\PVS\PVS_Report.xlsx")
'Cut Barcode Column and Insert it at beginning of sheet
Columns("J").Cut
Columns("A").Insert Shift:=xlToRight
Set pvsReport = Workbooks("PVS_Report.xlsx")
Set tracker = ThisWorkbook
Else
Set pvsReport = Workbooks("PVS_Report.xlsx")
Set tracker = ThisWorkbook
End If
k = beginningNum
Do Until k = endingNum + 1
tracker.Sheets(1).Cells(k, "D").Value = Application.VLookup(Cells(k, "B"), pvsReport.Sheets(1).Range("A:M"), 13, False)
tracker.Sheets(1).Cells(k, "A").Value = Application.VLookup(Cells(k, "B"), pvsReport.Sheets(1).Range("A:H"), 8, False)
tracker.Sheets(1).Cells(k, "C").Value = Application.VLookup(Cells(k, "B"), pvsReport.Sheets(1).Range("A:B"), 2, False)
If IsEmpty(Cells(k, "C").Value) Then
tracker.Sheets(1).Cells(k, "C").Value = Application.VLookup(Cells(k, "B"), pvsReport.Sheets(1).Range("A:C"), 3, False)
End If
tracker.Sheets(1).Cells(k, "E").Value = Application.VLookup(Cells(k, "B"), pvsReport.Sheets(1).Range("A:R"), 18, False)
tracker.Sheets(1).Cells(k, "F").Value = Application.VLookup(Cells(k, "B"), pvsReport.Sheets(1).Range("A:P"), 16, False)
k = k + 1
Loop
MsgBox "Done! c[_] "
Unload Me
End Sub
答案 0 :(得分:0)
也许您应该检查Application.VLookup
的第一个参数是否引用了正确的工作表,因为您在Cells(k, "B")
之前没有使用跟踪器或pvsReport。
当您打开工作簿时,活动工作表可能会更改,因此Cells(k, "B")
将从pvsReport
而不是tracker
获取数据。
如果pvsreport
已经打开,则活动工作表不会更改。
尝试更改:
Application.VLookup(Cells(k, "B"), ...
为:
Application.VLookup(tracker.Sheets(1).Cells(k, "B"), ...
答案 1 :(得分:0)
我将跟踪器更改为指向ThisworkBook.WorkSheets(1)
。 Application.Vlookup
可能有效,但您应该使用WorkSheetFunction.Vlookup
。我完全限定了你对Cells的引用(k,“B”)。您正在将列J插入到列A的左侧。这是ThisWorkbook.Worksheets(1)
上的那个。您也应该完全限定该引用。处理这种长引用时使用With语句可以使调试更容易。
Private Sub BarcodeVlookup(beginningNum As Integer, endingNum As Integer)
Dim pvsReport As Excel.Workbook
Dim tracker As Excel.Worksheet
Dim lookFor As Range
Dim brandRng As Range
Dim i As Integer
Dim k As Integer
Dim r As Integer
Dim j As String
Set tracker = ThisWorkbook.Worksheets(1)
If Not IsFileOpen("\\msfs05\Data1\SHARE\MDCM_Reports\LowesCom\PVS\PVS_Report.xlsx") Then
Workbooks.Open ("\\msfs05\Data1\SHARE\MDCM_Reports\LowesCom\PVS\PVS_Report.xlsx")
'Cut Barcode Column and Insert it at beginning of sheet
Columns("J").Cut
Columns("A").Insert Shift:=xlToRight
End If
Set pvsReport = Workbooks("PVS_Report.xlsx")
k = beginningNum
With pvsReport.Worksheets(1)
Do Until k = endingNum + 1
tracker.Cells(k, "D").Value = WorksheetFunction.VLookup(tracker.Cells(k, "B"), .Range("A:M"), 13, False)
tracker.Cells(k, "A").Value = WorksheetFunction.VLookup(tracker.Cells(k, "B"), .Range("A:H"), 8, False)
tracker.Cells(k, "C").Value = WorksheetFunction.VLookup(tracker.Cells(k, "B"), .Range("A:B"), 2, False)
If IsEmpty(Cells(k, "C").Value) Then
tracker.Cells(k, "C").Value = WorksheetFunction.VLookup(tracker.Cells(k, "B"), .Range("A:C"), 3, False)
End If
tracker.Cells(k, "E").Value = WorksheetFunction.VLookup(tracker.Cells(k, "B"), .Range("A:R"), 18, False)
tracker.Cells(k, "F").Value = WorksheetFunction.VLookup(tracker.Cells(k, "B"), .Range("A:P"), 16, False)
k = k + 1
Loop
End With
MsgBox "Done! c[_] "
Unload Me
End Sub