wsnyder
Board Regular
- Joined
- Sep 23, 2018
- Messages
- 224
- Office Version
- 365
- Platform
- Windows
Hi all,
Using Excel 365.
I am trying to find the last used column on the worksheet.
However, it is consistently returning 11 where I expect 10.
I highlighted column 11 to right and cleared all.
Still, the code returns 11.
What am I missing?
Thanks,
-w
Using Excel 365.
I am trying to find the last used column on the worksheet.
However, it is consistently returning 11 where I expect 10.
I highlighted column 11 to right and cleared all.
Still, the code returns 11.
What am I missing?
Thanks,
-w
VBA Code:
Sub foo()
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Long
Dim j As Long
Set wb = Workbooks("rpt.xlsx")
Set ws = wb.Worksheets(1)
i = 0
i = GetLastUsedColumn(ws:=ws)
j = GetFirstNonBlankCellInColumn(ws:=ws, _
col_number:=i)
Debug.Print "Last used column: "; i
Debug.Print "First Non-blank row: "; j
Set ws = Nothing
Set wb = Nothing
End Sub
Public Function GetLastUsedColumn(ws As Worksheet) As Long
'Purpose : Get last used column on a worksheet
'Parameters :
'1.) ws : Required parameter. A Worksheet Object.
'
'=============================================================================================================================
'=============================================================================================================================
GetLastUsedColumn = ws.Range("A1").SpecialCells(xlCellTypeLastCell).Column
End Function