How to find the last column which is not empty

moramramesh

New Member
Joined
May 22, 2019
Messages
17
I have the below code:

Range("A:A,B:B,C:C,D:D,E:E,F:F,G:G,H:H,I:I").Select
Range("I1").Activate
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("Sheet2!$A1:$I$" & lr2)


Since I know that data is available from columns A to I, I have passed all the columns in the range function. But I would like to pass range value dynamically based on the last column containing data.
Accordingly in the 2nd line, I want pass the last column first cell instead of Range("I1").Activate
Similarly in the 4th line, I want to pass dynamic column value in place of I, instead of Range("Sheet2!$A1:$I$" & lr2)
 
Range(ColumnLetter).Activate
You only have the column reference, no row reference.
If you want to select the whole column, use:
Code:
Columns(ColumnLetter).Activate
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You only have the column reference, no row reference.
If you want to select the whole column, use:
Code:
Columns(ColumnLetter).Activate
Besides the above comment by Joe, you will have one more correction to make in case you missed it. You may have caught it after having read Joe's response in Message #9 , but I can't be sure so I figured I would mention it just in case. On this line of code...

Range("A:ColumnLetter").Select

You need to write it like this...

Range("A:" & ColumnLetter).Select

again, for the reason Joe explained in Message #9 .
 
Upvote 0
It was my mistake, I was doing wrong concatenation earlier. Now I corrected it and completed my task: Range(ColumnLetter & "1").Activate
Thanks Rick, Joe and everyone for your help and suggestions. This site is awesome and very much helpful for novice users like me.
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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