我有一些VBA代码在运行时返回N / A,但是如果我在单元格中执行正常的Vlookup,它会返回正确的信息。
=VLOOKUP(A6,'[AR Aging Detail Report as of 06-26-2017.xlsx]Sheet1'!$H:$V,5,FALSE)
这是在单元格中工作的正常Vlookup。我有这个代码只返回所有单元格的N / A.我可以发布完整的代码,但这是相关的。 xlWS和xlWB2之前被定义为相关工作簿。
xlWS.Activate
With xlWS
For i = 2 To rCount
sFormula = Application.VLookup(Range("A" & i), xlWB2.Sheets(1).Range("H:V"), 5, False)
这里有什么问题? vlookup看起来是一样的。
为上下文添加完整代码。它首先找到带有“AR Aging *”作为名称的最新文件,并将其分配给xlWB2。那是那里的大部分代码。然后我尝试在每一行上运行Vlookup并将返回的值放在xlWB的列'O'中。
'Specify the path to the folder
MyPath = "C:\Users\TheAgency\Documents\test"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "AR Aging*.xlsx", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Set xlWB2 = Workbooks.Open(MyPath & LatestFile, True, True)
On Error Resume Next
' test if excel is already running
Set xlApp = GetObject(, "Excel.Application")
On Error GoTo 0
If Err.Number <> 0 Then
' Excel is not running
' so create an object
Set xlApp = CreateObject("Excel.Application")
Else
' flag. excel is already running before we execute this code
bolIsExcelRunning = True
End If
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(WB_PATH & "hdremittance.csv")
Set xlWS = xlWB.Sheets("hdremittance")
Set xlWS2 = xlWB2.Sheets(1)
xlSheetName = xlWS2.Name
rCount = xlWS.Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 1
Debug.Print "rCount : " & rCount
' rCount2: 6 Months Row Count
On Error Resume Next
rCount2 = xlWS2.Range("D:D").Cells.SpecialCells(xlCellTypeAllFormatConditions).Count - 1
Debug.Print "rCount2 : " & rCount2
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Sheet2"
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "returns"
Sheets.Add(after:=Sheets(Sheets.Count)).Name = ".com"
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Shortage"
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "No Store"
xlWS.Activate
With xlWS
For i = 2 To rCount
sFormula = Application.VLookup(.Range("A" & i), xlWB2.Sheets(1).Range("A:J"), 3, True)
rFormula = Application.VLookup(.Range("A" & i), xlWB2.Sheets(1).Range("A:J"), 2, False)
Inv = Application.VLookup(.Range("A" & i), xlWB2.Sheets(1).Range("A:V"), 6, False)
Debug.Print sFormula
.Range("O" & i).Value = sFormula
.Range("R" & i).Value = rFormula
.Range("S" & i).Value = "Wire Transfer"
.Range("T" & i).Value = "1200"
.Range("U" & i).Value = "4699"
.Range("V" & i).Value = "1100"
.Range("X" & i).Value = Inv
DoEvents
Next
End With
答案 0 :(得分:0)
也许您对Sheets(1)的引用不正确。我重建了你的程序,发现如果没有匹配则返回错误2042(#N / A)。您的公式清楚地表明您指的是“Sheet1”,但您的代码表示Sheets(1),这可能不一定是相同的。将“Sheets(1)”替换为“Sheets(”Sheet1“)”并再试一次。