Although a lot of posts explain how to assign a whole sheet to a variable, i couldn't find one that explains how to assign the name only to a sub/function argument.
I need to be able to do this because I want to easily switch between worksheets just by changing an argument in my sub/function.
I have 3 routines :
'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
In runthecode I call the Cell_Address Function. As you can see, I have a String variable named WorksheetName which I try to pass in the segment
With Sheets(WorksheetName)
Because I assume that it'd be the same thing as saying
With Sheets("Pass Rates")
However, it always seems to take my first worksheet (Pass Rates) and never goes to the second worksheet (Regression URLs) and gives me the error 91 "Object variable not set" (which is just because it can't find the next Runtype string (Text-Based Functional" in the first sheet since that string is only in the second sheet).
Does anyone know how I can pass the worksheet name in a function argument and use it to switch between sheets when calling that function ?
Thanks !
Cell_Address
, you have yourWith Sheets(WorksheetName)...End With
block - but then you immediately callFind_String
- _which has an implicit reference to theActiveSheet
- i.e.ActiveSheet.Cells.Find
.Cell_address
andFind_String
should be combined into 1Function
– BigBenWith
block. You could remove theWith
andEnd With
entirely and the code would execute exactly the same. – JNevillactivesheet
andselection
will cause all sorts of issues here. What is theactivesheet
at the time that this code executes? What is theselection
? Also you sayCells(RunType_Row, Language_Column)
but...Cells
of what... if you want that to be theCells()
of the sheet in yourWith
block then that should be.Cells(Runtype_Row, Language_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)
(notice the.
in the front) – JNevillCells()
again in yourFind_String
.Cells
of what range/sheet though? You are not being explicit and telling VBA whatCells
is referencing so it defaults toActiveSheet
. – JNevill