By using vba.booksticle.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

948 Steps to 6 Figures by Learning Excel-VBA and Other Skills



Step 267 - FindLastCol(1) - find the last column passed a row udf




Function FindLastCol(r As Long) As Long
'Version: 1.000
'Purpose: This routine will find the last row in the spread sheet for the passed column
'Hidden column aren't included
' c is the column to use to find the last row
Dim prevcol As Long

'Go to the end
If r <= 0 Then
    FindLastCol = 0
    Exit Function
    
End If

Cells(r, ActiveSheet.Cells.Columns.Count).Select

  Selection.End(xlToLeft).Select
prevcol = 0
Do While Trim(ActiveCell.Text) = "" And ActiveCell.Column <> 1
      Selection.End(xlToLeft).Select
      
      If ActiveCell.Column = prevcol Then
        FindLastCol = ActiveCell.Column
        Exit Function
      Else
        prevcol = ActiveCell.Column
        
      End If
    
Loop
If ActiveCell.Text = "" Then
    FindLastCol = 0
Else
    FindLastCol = ActiveCell.Column
End If

End Function


   Table of Contents | Send us your feedback | © 2025 All Rights Reserved | Edit