Get the last column of a selection

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. Windows
I want to store lastCol as a variable equal to the last column in the current selection

Code:
Sub selectionEDIT()
Dim sSelect As Range
Dim lastCol As Long
Dim lastRow As Long

Set sSelect = Selection
lastCol = sSelect.SpecialCells(xlCellTypeLastCell).Column
lastRow = sSelect.SpecialCells(xlCellTypeLastCell).Row

but with this code the range of the selection doesnt seem to be doing anything. it just counts the entirety of the worksheet's columns.
where if i have data from a1:m7 and i select d1:h5 the lastCol value should be 5 and the row be 5.

what do?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How about
Code:
lastCol = sSelect.Columns.Count + sSelect.Column - 1
lastRow = sSelect.Rows.Count + sSelect.Row - 1
 
Upvote 0
can you explain the logic behind the "+sSelect.Column - 1" ?
and why this works and every other form of this seems to fail in this situation?
works perfectly though, thank you.
 
Upvote 0
If you select C3:G7 the count of both columns & rows will give 5, so to get to 7 you have to add the 1st column/row number (in this case 3) which gives 8 & then subtract 1
 
Upvote 0
that is unnecessarily complicated :confused: oh excel. . .
thanks for the info Fluff.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
If you think that's complicated how about
Code:
Sub Blakeskate()
Dim sSelect As Range
Dim lastCol As Long
Dim lastRow As Long

Set sSelect = Selection
lastCol = Split(sSelect.Address(, , xlR1C1), "C")(2)
lastRow = Split(Split(sSelect.Address(, , xlR1C1), "R")(2), "C")(0)
End Sub
:)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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