在subs之间传递参数的问题

我有我的主要分如下所示:

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表“search”,并将列号转换为一个字母。 我正在寻找传递这些参数,因为我会在我的主要子目录中调用一些search和一些转换(一旦它正在工作)

自从我使用这种设置已经很长时间了,通常我只是调用过程而不传递参数,但是这样做会更加清爽。

任何帮助将非常感激。

使用Sub来设置全局variables不是一个好的编码模式 – 使用函数将值直接返回给调用代码会更好:

 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 

…和Rory指出的那样使用Set作为对象types的variables。