【问题标题】:Vlookup using VBA使用 VBA 查找
【发布时间】:2019-03-11 11:49:12
【问题描述】:

我有一本工作簿,其中包含我要添加到 word 中的信息。 现在我需要添加一个必须使用 vlookup(或索引匹配)找到的 txt

我的代码无法运行,6 小时后我快要放弃了,但希望你们能帮助我。

代码是:

wrdApp.Selection.TypeText Text:=(Application.Function.VLookup(Cells((RowNo + i), ColumnForLevel2Text).Value, sectionT.Range("ab1:ac5"), 2, 0))

我的 Vba 代码放在另一个工作簿中。但到目前为止,这还不是问题。

我想在单元格(Range((RowNo + i), ColumnForLevel2Text)中查找文本

我想在同一工作簿的另一张工作表中查找 range("ab1:ac5"),称为 sectionT。

我的文本可能是 1.1,我希望将文本“结果”输入到 word 中。

问候 彼得

【问题讨论】:

    标签: excel vba ms-word vlookup


    【解决方案1】:

    我不确定我是否有一切可以解决您的问题,但我可以帮助您将问题拆分为更易于管理的部分。如果你这样写代码:

    Dim LookupValue, TableArray, OutputText
    
    LookupValue = Cells((RowNo + i), ColumnForLevel2Text).Value
    Set TableArray = sectionT.Range("ab1:ac5")
    OutputText = Application.WorksheetFunction.VLookup(LookupValue, TableArray, 2, 0)
    
    wrdApp.Selection.TypeText Text:=OutputText
    

    您可以一步一步地跟踪它,看看哪里出了问题。这里最大的问题是你写的是Function而不是WorksheetFunction

    【讨论】:

    • 谢谢山姆。不幸的是,这仍然不起作用。我使用您的建议将其分解如下: Dim LookupValue As String Dim TableArray As String Dim OutputText As String LookupValue = Cells((RowNo + i), ColumnForLevel2Text).Value TableArray = sectionT.Range("a1:b5") OutputText = Application.WorksheetFunction.VLookup(LookupValue, TableArray, 2, 0) 'wrdApp.Selection.TypeText Text:=OutputText MsgBox (outputxt)
    • 嗨,山姆。非常感谢。代码现在已修改,但 Excel 返回并显示 TableArray = sectionT.Range("a1:b5") 行需要对象。我的完整代码是: Dim LookupValue As String Dim TableArray As String Dim OutputText As String LookupValue = Cells((RowNo + i), ColumnForLevel2Text).Value TableArray = sectionT.Range("a1:b5") OutputText = Application.WorksheetFunction.VLookup (LookupValue, TableArray, 2, 0) 'wrdApp.Selection.TypeText Text:=OutputText MsgBox (outputxt)。请注意,在尝试发送以添加到 word 之前,我使用 msgbox 进行了测试。有什么建议吗?
    • 我在 TableArray 之前错过了 Set
    • 对不起,它没有帮助。我是否声明正确 TableArray = String ?将 LookupValue 作为字符串调暗 TableArray 作为整数调暗 OutputText 作为字符串 LookupValue = Cells((RowNo + i), ColumnForLevel2Text).Value Set TableArray = sectionT.Range("a1:b5") OutputText = Application.WorksheetFunction.VLookup(LookupValue, TableArray , 2, 0) 'wrdApp.Selection.TypeText Text:=OutputText MsgBox (outputxt)
    猜你喜欢
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多