Selection of xltoleft and xltoright in vba.

How we can use xltoright and xltoleft for count whole columns of the sheet in VBA.
Nar SinghAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
I'm not sure what you are asking, but to count the number of columns on a sheet you can do

ColumnCount = Cells.Find(" * ", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
xlToRight is equivalent to Ctrl + Right Arrow Key.
So if your column headers are in Row1, select A1 and press Ctrl + Right Arrow Key, that will take you to the last filled column header if there are no blank columns otherwise it will take you to the last consecutive column header.
In VBA you can use it like...
Dim LastCol As Long
LastCol = Range("A1").End(xlToRight).Column

Open in new window

xlToLeft on the other hand is equivalent to the Ctrl + Left Arrow Key.
So if your column headers are in Row1, select the cell in last column available XFD1 and press Ctrl + Left Arrow Key, that will take you to the last filled column header in Row1.

In VBA you can use it like...
Dim LastCol As Long
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column   'Here Cells(1, Columns.Count) is equivalent to XFD1

Open in new window

Other than what Martin has suggested, you may also use the following line to find the last filled column on the sheets.
Dim LastCol As Long
LastCol = ActiveSheet.UsedRange.Columns.Count

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
UsedRange is unreliable to find the last column or row in a range. If cells outside the data contain formatting then they will be counted as part of the UsedRange giving an incorrect result. More reliable would be CurrentRegion, e.g

Dim Col as Long

Col = Sheet1.Cells(1,1).CurrentRegion.Columns.Count

Open in new window

Where the data is on Sheet1 and A1 is within the data on that sheet and the data is in  a complete block, i.e there are entirely  empty columns or rows then then use
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
The CurrentRegion property won't work if there are empty columns with no column headers also.

The best method to find the LastRow and LastColumn entirely depends upon the data layout on the sheet.
Roy CoxGroup Finance ManagerCommented:
That's why I said the data should be in a complete block with no empty columns or rows. Header rows are irrelevant.

Actually, I prefer to use Excel's Data Tables then simply count the ListRows or listColumns.

I agree there is no perfect method, it all depends on what the user needs the last column for and if using it with data storage then it depends on a well designed layout.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
All chosen answers contributed to resolve the original question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.