Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. 'INPUT : Sheet, the worksheet we'll search to find the last column
  3. ' : RowNum, the row (as a number) we're interested in
  4. 'OUTPUT : Long, the last occupied column in the specified row
  5. 'SPECIAL CASE : if a bad row number is entered (anything < 1), return 0
  6. 'EXAMPLES BELOW:
  7. '
  8. 'assume that on MySheet, cells A1:F1 are occupied,
  9. 'but B3 is the only occupied cell in row 3
  10. 'LastColNumInRow(MySheet, 3)
  11. '>> 2
  12. '
  13. 'assume that EmptySheet is totally empty
  14. 'LastColNumInRow(EmptySheet, 1)
  15. '>> 1
  16. '
  17. 'assume that you mistakely pass in -1 for the row number on MySheet
  18. 'LastColNumInRow(MySheet, -1)
  19. '>> 0
  20. '
  21. Public Function LastColNumInRow(Sheet As Worksheet, RowNum As Long) As Long
  22. If RowNum < 1 Then
  23. LastColNumInRow = 0
  24. Exit Function
  25. End If
  26. With Sheet
  27. LastColNumInRow = Cells(RowNum, .Columns.Count).End(xlToLeft).Column
  28. End With
  29. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement