Last Column Using VBA

Topdrop18

New Member
Joined
Dec 28, 2009
Messages
25
I have searched through the website and even Bill Jelen's book, but I still cannot find the answer as to how to write the code to find the last column with data in it. I have tried defining a range as seen below, but it's still not working. Any suggestions?

finalcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Welcome to the Board!

This will find the last column based on row 1, is that the row you are wanting to find the last column based on?

Maybe the column it is currently returning has a null string in it? What does this return and waht are you expecting?

Code:
msgbox Cells(1, Columns.Count).End(xlToLeft).Column
 
Upvote 0
Try:
Code:
lstcol = Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious, LookIn:=xlValues).Column
 
Upvote 0
So those really aren't giving me what I need. I essentially want to be able to define what the last row is so that I can then use a loop to run a formula, when the number of columns may change. Does that make sense?
 
Upvote 0
If you're clear about what you want from the start, you're much more likely to get it.

The line of code I suggested above does exactly what you initially specified.
 
Upvote 0
You asked for the last column when I think you meant row

Code:
LastRow = ActiveSheet.UsedRange.Find(what:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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