我试图在第一个空单元格/行中插入Vlookup值。 这是我的代码
Dim lookfor As Range
Dim searchrange As Range
Dim book1 As Workbook
Dim book2 As Workbook
Dim book2name As String
book2name = "Micros_export.xlsm"
Dim book2namepath As String
book2namepath = "C:\Users\pkechichian\Documents\Revamp\Micros_export.xlsm"
Set book1 = ThisWorkbook
Set book2 = Workbooks.Open(book2namepath)
Set lookfor = book1.Sheets("Actual").Cells(2, 4)
Set searchrange = book2.Sheets("Accpac").Range("E:H")
lookfor.Offset(2, 0).End(xlDown).Value = Application.VLookup(lookfor, searchrange, 3, False)
感谢您的帮助,感谢格式化!
答案 0 :(得分:0)
编辑
Option Explicit
Sub main()
Dim book2namepath As String
Dim searchrange As Range
book2namepath = "C:\Users\pkechichian\Documents\Revamp\Micros_export.xlsm"
Set searchrange = Workbooks.Open(book2namepath).Sheets("Accpac").Range("E:H") '<-- set range directly at workbook opening
With ThisWorkbook.Sheets("Actual") '<--| refer to "destination" sheet
.Cells(.Rows.Count, "D").End(xlUp).Offset(1).value = Application.VLookup(.Cells(2, 4).value, searchrange, 3, False) '<--| write lookup value in first empty cell below in column "D"
End With
ActiveWorkbook.Close False '<--| close opened workbook without saving changes
End Sub
答案 1 :(得分:0)
感谢您的快速回答!它工作正常,除了没有在正确的位置插入值。我添加了一些图片,以便您提供更好的主意。
The value should be inserted in "D4" This is where the value is inserted