将工作表名称分配给函数/子参数

时间:2018-08-02 19:15:15

标签: vba excel-vba

尽管有很多文章解释了如何将整个工作表分配给变量,但我找不到一个可以解释如何仅将名称分配给子/函数 argument < / em>。

我需要能够执行此操作,因为我想仅通过更改子/函数中的参数即可轻松地在工作表之间切换。

我有3个例程:

'Sub to run the code and call the other routines
Public Sub runthecode()

Dim ID As Range, Result As Range

ResultLocation = Cell_Address("Pass Rates", "Functional Text", "fra-FRA")
IDLocation = Cell_Address("Regression URLs", "Text-Based Functional", "fra-FRA")


MsgBox ResultLocation

End Sub

-------
' Sub to find, by 2 way look up, the address of a cell

Public Function Cell_Address(WorksheetName As String, Runtype As String, Language As String) As String

With Sheets(WorksheetName)

Call Find_String(Runtype)

Runtype_Row = Selection.Row

Call Find_String(Language)

Language_Column = Selection.Column

Cell_Address = Cells(Runtype_Row, Language_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)

MsgBox Cell_Address

End With

End Function

-------
' Sub to find a specific string

Public Sub Find_String(Search_String As String)

    Cells.Find(What:=Search_String, After:=ActiveCell, LookIn:=xlFormulas, 
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, 
MatchCase:=False, SearchFormat:=False).Activate

End Sub

runthecode 中,我调用 Cell_Address 函数。如您所见,我有一个名为WorksheetName的String变量,我尝试在该段中传递

With Sheets(WorksheetName)

因为我认为这和说的话是一样的

With Sheets("Pass Rates")

但是,它似乎总是会占用我的第一个工作表(通过率),而永远不会进入第二个工作表(回归URL),并给我错误91“未设置对象变量”(这仅仅是因为它找不到下一个Runtype字符串(第一页中的基于文本的功能”,因为该字符串仅在第二页中。)

有人知道我如何在函数参数中传递工作表名称,并在调用该函数时使用它在工作表之间切换?

谢谢!

2 个答案:

答案 0 :(得分:1)

这是尝试使其更接近我认为的意图:

'Sub to run the code and call the other routines
Public Sub runthecode()

    Dim ID As Range, Result As Range

    ResultLocation = Cell_Address("Pass Rates", "Functional Text", "fra-FRA")
    IDLocation = Cell_Address("Regression URLs", "Text-Based Functional", "fra-FRA")

    MsgBox ResultLocation

End Sub

-------
' Sub to find, by 2 way look up, the address of a cell

Public Function Cell_Address(WorksheetName As String, Runtype As String, Language As String) As String

    With Sheets(WorksheetName)
        Call Find_String(Runtype, Sheets(WorksheetName))
        Runtype_Row = Selection.Row     
        Language_Column = Find_String(Language, Sheets(WorksheetName))
        Cell_Address = .Cells(Runtype_Row, Language_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)
        MsgBox Cell_Address

    End With

End Function

-------
' Function to find a specific string and return the column in which it can be found

Public Function Find_String(Search_String As String, ws as worksheet) as Range 'returns column

    Find_String = ws.Cells.Find(What:=Search_String, After:=ActiveCell, LookIn:=xlFormulas, 
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, 
MatchCase:=False, SearchFormat:=False).Column

End Sub

我已将Find_String更改为一个函数,该函数将返回表示在其中找到字符串的Column的范围。这样,我们就不再依赖于激活和选择。

我们还将Find_String函数传递给我们感兴趣的工作表,以便它将知道您引用的Cells()

最后,With块现在在我们说.Cells()来设置Cell_Address的地方有用。

我不能保证这能完全满足您的要求,但是它可以使您克服现在面临的障碍,并且与最终目标无关。

答案 1 :(得分:0)

感谢@JNevill和@BigBen的反馈和解决方案;我已经全部工作了:)

这是工作代码:

Public Sub runthecode()

Dim http As Object, JSON As Object
Dim ID As String, Result As String
Dim IDLocation As String, ResultLocation As String

Application.ScreenUpdating = False

IDLocation = Cell_Address("Regression URLs", "Text-Based Functional", "fra-FRA")
ResultLocation = Cell_Address("Pass Rates", "Functional Text", "fra-FRA")


ID = Sheets("Regression URLs").Range(IDLocation).Value
Result = Sheets("Pass Rates").Range(ResultLocation).Value

MsgBox Result
MsgBox ResultLocation

End Sub
______________________

Public Function Cell_Address(WorksheetName As String, Runtype As String, Language As String) As String

    Call Find_String(Runtype, WorksheetName)

    Runtype_Row = Selection.Row

    Call Find_String(Language, WorksheetName)

    Language_Column = Selection.Column

    Cell_Address = Cells(Runtype_Row, Language_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)

End Function
______________________

Public Sub Find_String(Search_String As String, WorksheetName As String)

    Sheets(WorksheetName).Select

    Cells.Find(What:=Search_String, After:=ActiveCell, LookIn:= _
    xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False).Activate

End Sub

如您所见,我只是在查找字符串(在Find_String中)之前选择了工作表。这可能不是最优雅/最有效的解决方案,但至少可以奏效。

谢谢!