簡體   English   中英

在Word中使用Excel匹配function VBA

[英]Using Excel Match function in Word VBA

我有一個 Word VBA 宏,它可以完美地填充 Excel 工作簿(充當“數據庫”)中的數據。 它調用了 excel 索引和匹配函數。 但是,截至今天,我反復收到錯誤消息,提示找不到 Match 和 Index 函數。 發生了什么? 代碼如下。

On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")

If Err Then
    bStart = True
    Set xlapp = CreateObject("Excel.Application")
End If

On Error GoTo 0

Set xldocbook = xlapp.Workbooks.Open("D:/Dr list.xlsx")
Set xldocsheet = xldocbook.Worksheets(1)

'Set the current selection
With xlapp.WorksheetFunction

    IUH = .Index(xldocsheet.Range("I:I"), .Match(DrName, xldocsheet.Range("E:E"), 0))

    If IUH = "N" Then
        Address = .Index(xldocsheet.Range("K:K"), .Match(DrName, xldocsheet.Range("E:E"), 0))
        CityStateZip = .Index(xldocsheet.Range("L:L"), .Match(DrName, xldocsheet.Range("E:E"), 0))
    Else
        Address = ""
        CityStateZip = ""
    End If

    Phone = .Index(xldocsheet.Range("F:F"), .Match(DrName, xldocsheet.Range("E:E"), 0))

    Fax = .Index(xldocsheet.Range("G:G"), .Match(DrName, xldocsheet.Range("E:E"), 0))

    FullAddress = Address + " " + CityStateZip

    Debug.Print "Full Address is" + FullAddress

End With

xldocbook.Close

If bStart = True Then
    xlapp.Quit
End If

用 application 替換 Worksheetfunction 可能會有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM