在潜艇之间传递参数有问题



我有我的主子标题如下:

Option Explicit
Public y As String
Public xCell As Range
Sub BenAppMr()
Call SearchFor("Award")
Call ConvertToLetter(xCell)
MsgBox "The column letter is " & y
End Sub

然后是我从上面调用的另外两个子字符:

Sub SearchFor(z As String)
xCell = Cells.Find(What:=z, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
End Sub

Sub ConvertToLetter(x As Range)
y = Split(x.Address(1, 0), "$")(0)
End Sub

我错过了什么吗?我不太明白为什么这行不通。

我想在我的excel表格中搜索"奖项",并将列号转换为字母。我希望通过这些参数,因为我将调用几个搜索和几个转换在我的主子(一旦它的工作)

我已经很长时间没有使用这种设置了,通常我只是调用过程而不传递参数,但这样会更简洁。

使用Sub's来设置全局变量不是一个好的编码模式-您最好使用函数将值直接返回给调用代码:

Sub BenAppMr()
    Dim y As String, xCell As Range
    Set xCell = SearchFor("Award")
    If Not xCell Is Nothing Then
        y = ConvertToLetter(xCell)
        MsgBox "The column letter is " & y
    Else
        MsgBox "Search value not found!"
    End If
End Sub
Function SearchFor(z As String) As Range
    Dim xCell As Range
    Set xCell = ActiveSheet.Cells.Find(What:=z, After:=ActiveCell, LookIn:= _
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False)
    Set SearchFor = xCell
End Function
Function ConvertToLetter(x As Range) As String
    ConvertToLetter = Split(x.Address(1, 0), "$")(0)
End Function

…并使用Set作为对象类型变量,如Rory所指出的。

最新更新