Select all cells in a row to the last column header, even if not contiguous

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I need code that selects all columns in a row to the last column header, regardless of what cell I start in.
I tried the macro recorder...

VBA Code:
  Range(Selection, Selection.End(xlToRight)).Select
  Range(Selection, Selection.End(xlToRight)).Select
  Range(Selection, Selection.End(xlToRight)).Select

...but depending on which column I start in, the number of times I would need to use...

VBA Code:
Range(Selection, Selection.End(xlToRight)).Select

...would vary, so I can't use it.


I figured examples would further help to explain what I am looking for, see below...
Note that the grey cells would be the selected cells

Select all cells in a row to the last column header, Code, Before and After, V3.png


All the same would be true if I started in column B (or whatever other column)

Some Notes:
>In the examples above, there are 6 columns, but the number of columns and the column starting point will vary, so the code would need to reference the last column header as "Discount" and not as "column F"

>This would be from the active workbook and active worksheet, whatever the names would be.


I hope this isn't too challenging. I searched for over an hour on the forum this morning and to my surprise, I couldn't find anything that matched my needs.

The only pieces of code I found and tried to use is the below but I just couldn't get the syntax correct...

VBA Code:
Sub Select_All_Rows_to_last_column()
Set wk = activeworkbook
Dim lc As Long
lc = ws.Columns.End(xlToRight).Column

Range("A2").Select
Range(ws.Cells(lc)).Select

End Sub


Thanks much for any help anyone can provide!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe the below will help:
VBA Code:
Sub test()
    Dim LastHead As Long
    
    LastHead = Cells(1, Columns.Count).End(xlToLeft).Column
    Range(Selection, Cells(Selection.Row, LastHead)).Select
End Sub
 
Upvote 1
Are you looking for something like this:
VBA Code:
Range(Selection, Cells(Selection.Row, Cells(1, Columns.Count).End(xlToLeft).Column)).Select
 
Upvote 1
Wow, that was fast and they both worked! Thank you both!

What if I want to find the last column with data by column name instead?
 
Upvote 0
VBA Code:
Range(Selection, Cells(Selection.Row, Application.Match(data, Range(Cells(1, 1), Cells(1, Columns.Count).End(xlToLeft).Column), 0))).Select

I haven't tested but this may work.
 
Upvote 0
Mine would be:
VBA Code:
Sub test()
    Dim LastHead As Long
   
    LastHead = Rows(1).Find("Word").Column
    Range(Selection, Cells(Selection.Row, LastHead)).Select
End Sub

Or

VBA Code:
Sub test()
    Range(Selection, Cells(Selection.Row, Rows(1).Find("Word").Column)).Select
End Sub
 
Upvote 1
Solution
Thanks to both!

The answer is this one here...

VBA Code:
Sub test()
    Range(Selection, Cells(Selection.Row, Rows(1).Find("Word").Column)).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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