Last Column Is 1 More Than Expected

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. 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

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
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
If you press Ctrl+End on the sheet in question, which column is the selected cell in?
 
Upvote 0
Hi, for a smart worksheet only (other ways for dumb one) a VBA demonstration :​
VBA Code:
Sub Demo1()
        Dim C%
    With Workbooks("rpt.xlsx").Sheets(1).UsedRange.Columns
        C = .Item(.Count).Column
    End With
        MsgBox "Last column is #" & C
End Sub
 
Upvote 0
Hi, for a smart worksheet only (other ways for dumb one) a VBA demonstration :​
VBA Code:
Sub Demo1()
        Dim C%
    With Workbooks("rpt.xlsx").Sheets(1).UsedRange.Columns
        C = .Item(.Count).Column
    End With
        MsgBox "Last column is #" & C
End Sub
Thanks Marc,

What is the c%?
I've never seen that.

Thanks,
-w
 
Upvote 0

It's the character data type declaration as you can check in the VBE Locals windows like in VBA help for pretty each data type …​
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top