我试图提示用户输入,然后从Excel报告中查找该信息。并填充Word文档,但我不确定错误在哪里。我尝试基于this question和this question的代码。
它给了我运行时错误'438' Object不支持此属性或方法。我知道这是我使用这种方法的方式,你能指点我正确的方向吗?谢谢!
Sub PopulateForm()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim cin_number As String
Dim result As String
' Prompt user for input
cin_number = InputBox("Please enter the CIN#", "Input")
' Open the cover sheet letter
Set exWb = objExcel.Workbooks.Open("U:\HRA Cover Sheet Data.xls")
' Perform the VLookup...
result = objExcel.WorksheetFunction.VLookup(cin_number, _
exWb.Range("A:F"), 5, False)
' Testing the output
MsgBox result
exWb.Close
Set exWb = Nothing
End Sub
我正在使用Word 2003和Window XP。
答案 0 :(得分:1)
您正在尝试使用exWb
范围作为工作簿,而不是工作表。尝试
result = objExcel.WorksheetFunction.VLookup(cin_number, _
exWb.ActiveSheet.Range("A:F"), 5, False)