VBA与ActiveSheet的匹配功能

时间:2017-01-09 10:52:23

标签: vba

如果我放置:ThisWorkbookActiveWorkbook不起作用,脚本可以正常工作。

错误1004说:“Unable to get the Match property of the Worksheet function class

Dim dat As Date

calea_livrat = "my link" & ".xlsx"
Workbooks.Open calea_livrat

With ActiveWorkbook
    dat = zi & "-" & luna & "-" & an
    data_gen = CDbl(dat)
    nr_linie = Application.WorksheetFunction.Match(data_gen, ActiveWorkbook.Worksheets("PE_Centralizare").Range("A:A"), 0)
    MsgBox nr_linie
End With

这里出了点问题:Application.WorksheetFunction.Match(data_gen, ActiveWorkbook.Worksheets("PE_Centralizare").Range("A:A"), 0)但我无法弄清楚是什么。

1 个答案:

答案 0 :(得分:1)

这不是答案,只是如何正确使用Match函数:

Dim nr_linie As Variant

With ActiveWorkbook
    dat = zi & "-" & luna & "-" & an
    data_gen = CDbl(dat)

    ' in case Match was able to find data_gen in Column A
    If Not IsError(Application.Match(data_gen, .Worksheets("PE_Centralizare").Range("A:A"), 0)) Then
        nr_linie = Application.Match(data_gen, .Worksheets("PE_Centralizare").Range("A:A"), 0)
        MsgBox "Row number " & data_gen & " value was found in row " & nr_linie
    Else ' <-- Macth failed, unable to find find data_gen in Column A
        MsgBox data_gen & " value not found in Range !"
    End If
End With