Last column in a particular row

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have these functions for finding the last row and columns:

Code:
Public Function LRow(ByRef wks As Worksheet) As Long

    On Error GoTo Correction

        With wks
        
            LRow = wks.Cells.Find(What:="*", _
                                  After:=.Cells(.Rows.Count, .Columns.Count), _
                                  SearchDirection:=xlPrevious, _
                                  SearchOrder:=xlByRows).Row
            
        End With
    
Exitpoint:

    On Error GoTo 0
    
    Exit Function
    
Correction:

        LRow = 1

        Resume Exitpoint

End Function

Public Function LRowInCol(ByRef wks As Variant, _
                          ByRef Col As Variant) As Long

    On Error GoTo Correction

        If TypeName(wks) = "String" Then Set wks = Worksheets(wks)

        LRowInCol = wks.Columns(Col).Find(What:="*", _
                                          LookIn:=xlFormulas, _
                                          SearchOrder:=xlRows, _
                                          SearchDirection:=xlPrevious, _
                                          SearchFormat:=False).Row
                                          
Exitpoint:
    
    On Error GoTo 0
    
    Exit Function

Correction:

        LRowInCol = 1
    
        Resume Exitpoint
    
End Function

Public Function LCol(ByRef wks As Worksheet) As Long

    On Error GoTo Correction

        With wks
        
            LCol = .Cells.Find(What:="*", _
                               After:=.Cells(.Rows.Count, .Columns.Count), _
                               SearchDirection:=xlPrevious, _
                               SearchOrder:=xlByColumns).Column
            
        End With
    
Exitpoint:
    
    On Error GoTo 0
    
    Exit Function

Correction:

        LCol = 1
    
        Resume Exitpoint
        
End Function

How can I adapt them to find the last col for a specific row?

Thanks
 
Thanks Mark!
I knew I saw something like that some time ago, just couldn't remember the details.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I typically just use "Long" for everything (and see a lot of other experienced programmers do the same).
Thanks Mark!
I knew I saw something like that some time ago, just couldn't remember the details
You're welcome and I'm the same in as much as I haven't used Integer for a long time (no pun intended).
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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