从Excel公式转换为VBA代码

时间:2018-11-20 18:30:17

标签: excel vba

VBA中的以下Excel代码等效于什么?

=MATCH(A1,Ranged_Name,0)

其中Ranged_Name是工作簿级别的范围名称。

3 个答案:

答案 0 :(得分:4)

要再添加一个:

Dim r
With Activesheet
    r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with

If Not IsError(r) Then
    'got a match
End If

答案 1 :(得分:1)

如果工作表是ActiveSheet,则可以使用Range()。Find或Evaluate()进行匹配。

答案 2 :(得分:1)

在工作表单元格和VBA中:

Sub dural()
    MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub

enter image description here

EDIT#1:

根据评论,更好的选择:

Sub dural()
    Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction

    Set r1 = Sheets("Sheet1").Range("A1")
    Set r2 = Range("Range_Name")
    Set wf = Application.WorksheetFunction

    v = wf.Match(r1, r2, 0)
    MsgBox v
End Sub

并使用Find()

Sub larud()
    Dim r1 As Range, r2 As Range, v As Variant

    Set r1 = Sheets("Sheet1").Range("A1")
    Set r2 = Range("Range_Name")

    v = r2.Find(What:=r1.Value, After:=r2(1)).Row

    MsgBox v
End Sub