2

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
5
  • 2
    findLastRow has a Tabname argument... Commented Aug 22, 2018 at 16:57
  • 3
    Why do you expect the code to pass a string if you didn't pass anything here? Set Dimensions = findlastrow() Commented Aug 22, 2018 at 16:57
  • the solution could be: Function findlastrow(Optional ByVal Tabname as String) As Collection By the way, to know the last row or last column you can use: xlWorksheet.UsedRange.Rows.Count or xlWorksheet.UsedRange.Columns.Count Commented Aug 22, 2018 at 16:59
  • Couldnt really find how to do that, tried putting in the tabname variable in but did not work Commented Aug 22, 2018 at 17:00
  • @simo was trying to avoid ByVal usage. Commented Aug 22, 2018 at 17:29

1 Answer 1

4

you have defined Tabname as the argument for your findlastrow function and you have tried to define it again inside the function, you cannot do that, so

remove this line: Tabname As String

and amend the sub as explained below:

Sub SheetCopier()

    Dim TargetlRow As Long
    Dim TargetlCol As Long
    Dim Tabname As String

    Dim Dimensions As Collection
'this line is the problem since you are not passing a worksheet to the function
    'Set Dimensions = findlastrow()

 TargetControlTab = "tab1"
 Tabname = TargetControlTab


 set Dimensions=findlastrow(Tabname) 

 MsgBox "Last Row: " & Dimensions.Item(1) & vbNewLine & "Last Column: " & Dimensions.Item(2)
end sub
Sign up to request clarification or add additional context in comments.

8 Comments

@BigBen just noticed it and fixed it
That duplicate declaration was going to be OP's next compile error =)
@MathieuGuindon the sub does not have end sub too, LOL
@MathieuGuindon if you mean the "tabname as string" in the function? fixed, if not can you please elaborate?
@tengee I mean the compiler starts at the top and chokes on the first compile error it encounters - in your case a parameterless procedure call against a procedure requiring a parameter. The next compile error was going to be the duplicate declaration of Tabname in the scope of findLastrow, which this answer fixes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.