Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danwagnerco/1de2c416dd7b82e01e59 to your computer and use it in GitHub Desktop.
Save danwagnerco/1de2c416dd7b82e01e59 to your computer and use it in GitHub Desktop.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last column
'OUTPUT : Long, the last occupied column
'SPECIAL CASE : if Sheet is empty, return 1
'EXAMPLES BELOW:
'
'assume that there is a single entry on MySheet in cell C3
'LastColNum(MySheet)
'>> 3
'
'assume that EmptySheet is totally empty
'LastColNum(EmptySheet)
'>> 1
'
Public Function LastColNum(Sheet As Worksheet) As Long
With Sheet
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
LastColNum = Sheet.Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Else
LastColNum = 1
End If
End With
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment