returning last column of active range in VBA

bolo

Active Member
Joined
Mar 23, 2002
Messages
423
Hi,

Imagine you have selected a range A1:C4.

How do you return the last column of the selected range in VBA. = IE 3 in the above example

I have tried using activesheet.usedrange but this only seems to return the First column.
Thanks

Bolo
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try


Code:
Sub tst()
Dim r As Range, x As Integer
Set r = Range("A1:C4")
x = r.Columns.Count + r.Cells(1, 1).Column - 1
Debug.Print x
End Sub
 
Upvote 0
Hi,

Imagine you have selected a range A1:C4.

How do you return the last column of the selected range in VBA. = IE 3 in the above example

I have tried using activesheet.usedrange but this only seems to return the First column.
Thanks

Bolo
<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> LastCol()<br>    <SPAN style="color:#00007F">Dim</SPAN> lc <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">With</SPAN> Selection<br>        lc = .Column + .Columns.Count - 1<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,203,108
Messages
6,053,561
Members
444,673
Latest member
Jagadeshrao

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