Calling Functions, Last Row, Last Column

niki1210

New Member
Joined
Dec 10, 2018
Messages
4
I am having a heck of a time finding the last columns and rows. I know I'm missing something simple and I've gotten this far with some help (Thanks KD!) but I still can't find the missing link. I see and understand all the articles, but I'm missing something obvious.

I have the following but it's giving me an error. Compile error, expected array" lstRow = lastRow(ws, "J")

This is in Module 4 I tried using a personal excel macro workbook but that isn't working either.

Code:
Option Explicit
Public Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
    With ws
        lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
End Function


Public Function LastColumn(ws As Worksheet, Optional rowNum As Long = 1)
    With ws
        LastColumn = .Cells(rowNum, .Columns.Count).End(xlToLeft).Column
    End With
End Function

This is in Module 3

Code:
Sub Module3()

Doing some other things and then 

Dim ws As Worksheet
Dim sortRng As Range, rngL As Range, rngJ As Range
Dim lstRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lstRow = lastRow(ws, "J")


With ws


    Set sortRng = .Range("B11", .Cells(lstRow, LastColumn(ws, 11)))
    Set rngJ = .Range("J11:J" & lstRow)
    Set rngL = .Range("L11:L" & lstRow)


    With .Sort
        .SortFields.Clear
        .SortFields.Add Key:=rngJ, SortOn:=xlSortOnValues, Order:=xlAscending, _
                CustomOrder:="AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal
        .SortFields.Add Key:=rngL, SortOn:=xlSortOnValues, Order:=xlDescending, _
                DataOption:=xlSortNormal
        .SetRange sortRng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


End With

How do I call functions? I'm sorry but I'm just not getting it. Thanks!!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You might need to look at the "Doing some other things and then" part as the below runs fine for me when in the same regular module or a different module in the same workbook....

Code:
Sub Module3()

'Doing some other things and then

Dim ws As Worksheet
Dim sortRng As Range, rngL As Range, rngJ As Range
Dim lstRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lstRow = lastRow(ws, "J")
Debug.Print lstRow

End Sub

Public Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
    With ws
        lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
End Function


Public Function LastColumn(ws As Worksheet, Optional rowNum As Long = 1)
    With ws
        LastColumn = .Cells(rowNum, .Columns.Count).End(xlToLeft).Column
    End With
End Function
 
Last edited:
Upvote 0
Thank you! I never thought to take that into consideration. It doesn't error out now. However, it's not sorting. I made sure the columns are correct in the worksheet. Any ideas? I even tried commenting out the number sort.

Code:
Dim ws As Worksheet
Dim sortRng As Range, rngL As Range, rngJ As Range
Dim lstRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lstRow = lastRow(ws, "J")




With ws




    Set sortRng = .Range("B11", .Cells(lstRow, LastColumn(ws, 11)))
    Set rngJ = .Range("J11:J" & lstRow)
    Set rngL = .Range("L11:L" & lstRow)




    With .Sort
        .SortFields.Clear
        .SortFields.Add Key:=rngL, SortOn:=xlSortOnValues, Order:=xlAscending, _
                CustomOrder:="AmEx,Discover,Master Card,Visa,Check", DataOption:=xlSortNormal
'        .SortFields.Add Key:=rngJ, SortOn:=xlSortOnValues, Order:=xlDescending, _
'                DataOption:=xlSortNormal
        .SetRange sortRng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With




End With
 
Upvote 0
Code:
Option Explicit
Public Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
    With ws
        lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
End Function


Public Function LastColumn(ws As Worksheet, Optional rowNum As Long = 1)
    With ws
        LastColumn = .Cells(rowNum, .Columns.Count).End(xlToLeft).Column
    End With
End Function
You might find these two alternate functions to be useful as they will find the last row no matter what column it occurs in and the last column no matter what row it occurs in...
Code:
[table="width: 500"]
[tr]
	[td]Public Function LastRow(ws As Worksheet) As Long
  LastRow = ws.Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
End Function

Public Function LastColumn(ws As Worksheet, Optional ReturnLetter As Boolean) As Variant
  LastColumn = ws.Cells.Find("*", , xlValues, , xlByColumns, xlPrevious, , , False).Column
  If ReturnLetter Then LastColumn = Split(Cells(1, LastColumn).Address, "$")(1)
End Function[/td]
[/tr]
[/table]
NOTE: I added an optional second argument to the LastColumn function, a Boolean, which if True will return the column letter designation and which if False or omitted will return the column number.
 
Upvote 0
Thank you! That will come in very handy. I would have had to hard code each spreadsheet. You saved me a ton of time. I appreciate it. It's working now. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top