如何在Word VBA中使用VLookup?

问题描述 投票:0回答:1

我试图提示用户输入,然后从Excel报告中查找该信息。并填充Word文档,但我不确定错误在哪里。我试图从this questionthis question编写代码。

它给我运行时错误'438'对象不支持此属性或方法。我知道这是我使用这种方法的方式,你能指点我正确的方向吗?谢谢!

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。

vba ms-word word-vba word-2003
1个回答
1
投票

您正在尝试使用一系列exWb作为工作簿,而不是工作表。尝试

result = objExcel.WorksheetFunction.VLookup(cin_number, _
    exWb.ActiveSheet.Range("A:F"), 5, False)
© www.soinside.com 2019 - 2024. All rights reserved.