Need assistance with basic task: find last occupied column (using .End(xlToLeft).Column?)

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
876
Office Version
  1. 365
Platform
  1. Windows
Team Mr. Excel

I am trying to locate the last occupied column. I thought that I knew how to do it. But I cannot figure this simple task out.

I have a one-celled range name for the upperleftmost cell in the data range (cell O4). In code a VBA range object named rTargetCell that points to that cell. Data has 12 columns. There is nothing to the the right of the data.

Incidentally I have nine columns of user data in columns A through I.

VBA Code:
    Dim rTargetCell As Range
    
    Dim iLastColumn As Long
    
    Set rTargetCell = [ContactsData].Range("Header_FirstNameData")

    iLastColumn = rTargetCell.End(xlToLeft).Column

    Debug.Print "iLastColumn = " & iLastColumn

So, I am hoping for code to return the number 26 as the last column. (Column Z is the last one with data.) But Debug.Print reports column 9.

I appreciate your assistance!

Jim
 
What do you get with the line below?
VBA Code:
Debug.Print Cells(rTargetCell.Row, Columns.Count).End(xlToLeft).Column
 
Upvote 0
Solution

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