VBA找到找到的值的位置坐标

时间:2014-05-20 15:34:07

标签: excel vba excel-vba excel-2010

我有这个简单的macro,它会转到值所在的位置。

Sub dkdk()
    Dim dk As String
    Dim rng
    dk = "Document Type"
    If Trim(dk) <> "" Then
        With Sheets(1).Range("1:10")
            Set rng = .Find(dk, .Cells(.Cells.Count), xlValues, xlWhole, xlByRows, xlNext, _
            False)
            Application.Goto rng, True
            If  Then

            End If
        End With
    End If
End Sub

如何在[{1}}网格中接收msgbox位置即Cells(x,y)的位置?

2 个答案:

答案 0 :(得分:2)

这是你想要的吗?

Sub dkdk()
    Dim dk As String
    Dim rng As Range

    dk = "Document Type"

    If Trim(dk) <> "" Then
        With Sheets(1).Range("1:10")
            Set rng = .Find(dk, .Cells(.Cells.Count), xlValues, xlWhole, _
                      xlByRows, xlNext, False)

            If Not rng Is Nothing Then
                Application.Goto rng, True
                '<~~ This will give something like $A$1
                MsgBox rng.Address 
                '<~~ This will give something like Cells(1,1)
                MsgBox "Cells(" & rng.Row & "," & rng.Column & ")"
            End If
        End With
    End If
End Sub

答案 1 :(得分:1)

在Application.Goto语句之前的

,插入以下内容:

msgbox "rownumber: " & rng.row & ", columnnumber: " & rng.column

并且,如果您想错误检查:

if not rng is nothing then
    msgbox "rownumber: " & rng.row & ", columnnumber: " & rng.column
end if