For some reason the following code is not passing a string to the function but returning the collection very well if string variables are hardcoded to the function.
I get following error
Compile error: Argument not optional
pointing to the findlastrow() in
Set Dimensions = findlastrow() --line 8
Here is the code.
The main sub:
Sub SheetCopier()
Dim TargetlRow As Long
Dim TargetlCol As Long
Dim Tabname As String
Dim Dimensions As Collection
Set Dimensions = findlastrow()
TargetControlTab = "tab1"
Tabname = TargetControlTab
Call findlastrow(Tabname)
MsgBox "Last Row: " & Dimensions.Item(1) & vbNewLine & "Last Column: " & Dimensions.Item(2)
End Sub
The function:
Function findlastrow(Tabname As String) As Collection
'Finds the last non-blank cell in a single row or column
Dim FilledDimensions As Collection
Set FilledDimensions = New Collection
Sheets(Tabname).Select
'Find the last non-blank cell in column A(1)
TargetlRow = Cells(Rows.Count, 1).End(xlUp).Row
'Find the last non-blank cell in row 1
TargetlCol = Cells(1, Columns.Count).End(xlToLeft).Column
FilledDimensions.Add TargetlRow
FilledDimensions.Add TargetlCol
Set findlastrow = FilledDimensions
End Function
findLastRowhas aTabnameargument...Set Dimensions = findlastrow()Function findlastrow(Optional ByVal Tabname as String) As CollectionBy the way, to know the last row or last column you can use:xlWorksheet.UsedRange.Rows.CountorxlWorksheet.UsedRange.Columns.Count