如何在宏中使用通过宏找到的变量

How To Use Variable Found With Macro Within Macro

我有一个子程序查找包含特定字符串的单元格的地址。我将此单元格的地址捕获为变量 x,然后我想 select 该单元格。到目前为止我所拥有的是:

Call FindCell("n")
Sheets("Lagrange").Range(X).Select

变量 XFindCell 子例程返回的内容 "$D"

我认为您使用 SUB 来设置变量值被绊倒了。 Function 是解决您的问题的正确方法。

Sub yourMacro()
Const someText = "something to find"

findMyCell(someText).Select   

End Sub


Private Function findMyCell(textToFind As String) As Range


    Set findMyCell = ws.UsedRange.Find(textToFind, LookIn:=xlFormulas)


End Function

这是一个带有变量的示例:

Sub yourMacrowithVariable()
Const someText = "something to find"

Dim aRange As Range
    Set aRange = findMyCell(someText)

aRange.Select
'or Range(aRange.Address).Select


End Sub