1

I don't think I'm searching the right terms as I imagine this answer has to be somewhere but I am new to functions and trying to figure out how to get this to work:

How can I get my function findLastRow to be used where I say Range("B2" & findLastRow).Select ? The current error I recieve is Argument Not Optional and it highlights findLastRow

Public Function findLastRow(col As String) As Long 'making the macro available to all Sub Procedures

        Dim lastRow As Long
        With ActiveSheet
            lastRow = ActiveSheet.Cells(1048576, col).End(xlUp).Row 'finding the last possible row disregarding blanks
        End With

        findLastRow = lastRow
End Function

Sub Open_INV()

    ChDir "C:\Users\MonthEnd_Close\Import_Files"
    Workbooks.OpenText Filename:= _
        "C:\Users\MonthEnd_Close\Import_Files\INV", _
        Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
        , Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True

    On Error Resume Next

    Range("B2").Formula = "=TRIM(A2)"
    Range("B2").Copy
    Range("B2:B" & findLastRow).Select
    Selection.PasteSpecial xlPasteFormulas
End Sub
3
  • 1
    maybe Range("B2:B" & findLastRow).Select ? Commented Jul 18, 2018 at 17:18
  • Fixed that part, thank you! However I still receive the error "Argument Not Optional" Commented Jul 18, 2018 at 17:21
  • 1
    In calling the formula in your Sub, you are omitting the col argument. Range("B2:B" & findLastRow("B")) Commented Jul 18, 2018 at 17:22

1 Answer 1

1

Don't use ActiveSheet in a function. You have a perfect opportunity to pass in a range that has a defined parent worksheet rather than a string representing the column letter.

Public Function findLastRow(col As range) As Long 'making the macro available to all Sub Procedures

        Dim lastRow As Long
        With col.parent
            lastRow = .Cells(.rows.count, col.column).End(xlUp).Row 'finding the last possible row disregarding blanks
        End With

        findLastRow = lastRow

End Function

Sub Open_INV()
    Workbooks.OpenText Filename:= _
        "C:\Users\MonthEnd_Close\Import_Files\INV", _
        Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
        , Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True

    with activeworkbook.worksheets(1)
        .Range("B2:B" & findLastRow(.range("A2"))).Formula = "=TRIM(A2)"
    end with
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

I received an "Invalid Qualifier" error for findLastRow
Looks like I missed a closing bracket ad you should probably be looking for the last row in column A, not column B. Use the last row from column A to extend the range in column B.
@Jeeped: Would col.End(xlUp).Row be just as valid?
@AJD Only if called as findLastRow(.cells(.rows.count, "A"))

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.