我已经尝试了这个vba代码,但它只是不工作,我无法看到错误。这是代码
Sub RetrieveData()
Dim i As Long
Dim targetWorkbook As Workbook
Dim targetWorksheet As Worksheet
Dim targetFilePath As String
Dim targetFileName As String
' Set the target file path and name
targetFilePath = "\\kcjmserver\E\Data\Overseas Projects\Honilac Nutrition Limited\2023\Amazon reconciliation\D05 May 2023\Vlookup"
targetFileName = "InventoryItems-20230605.xlsx"
' Open the target workbook
Set targetWorkbook = Workbooks.Open(targetFilePath & "\" & targetFileName)
' Set the target worksheet
Set targetWorksheet = targetWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with the actual sheet name
' Loop through the data in Sheet1
For i = 2 To 26
' Perform the VLOOKUP
Worksheets("Sheet1").Cells(i, 10).Value = Application.VLookup(Worksheets("Sheet1").Cells(i, 6).Value, targetWorksheet.Range("A:M"), 9, False)
Worksheets("Sheet1").Cells(i, 7).Value = Application.VLookup(Worksheets("Sheet1").Cells(i, 6).Value, targetWorksheet.Range("A:M"), 3, False)
Next i
' Close the target workbook
targetWorkbook.Close SaveChanges:=False
' Release the memory
Set targetWorksheet = Nothing
Set targetWorkbook = Nothing
End Sub
我已经尝试了替代代码,似乎没有工作,我想从另一个工作簿中获取数据
1条答案
按热度按时间kkbh8khc1#
将
ThisWorkbook
添加到对Worksheets("Sheet1")
的引用。