Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. 'INPUT : Sheet, the worksheet we'll search to find the last column
  3. 'OUTPUT : Long, the last occupied column
  4. 'SPECIAL CASE : if Sheet is empty, return 1
  5. 'EXAMPLES BELOW:
  6. '
  7. 'assume that there is a single entry on MySheet in cell C3
  8. 'LastColNum(MySheet)
  9. '>> 3
  10. '
  11. 'assume that EmptySheet is totally empty
  12. 'LastColNum(EmptySheet)
  13. '>> 1
  14. '
  15. Public Function LastColNum(Sheet As Worksheet) As Long
  16. With Sheet
  17. If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
  18. LastColNum = Sheet.Cells.Find(What:="*", _
  19. After:=.Range("A1"), _
  20. LookAt:=xlPart, _
  21. LookIn:=xlFormulas, _
  22. SearchOrder:=xlByColumns, _
  23. SearchDirection:=xlPrevious).Column
  24. Else
  25. LastColNum = 1
  26. End If
  27. End With
  28. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement