variable Row Selection

jgarland

New Member
Joined
Nov 6, 2005
Messages
43
Hi All,

I'm trying to find the correct vba syntax to select a row starting from the active cell (variable) out to a named column. The named column location will vary because additional columns will be inserted between it and the active cell.

Example: select active cell A5 (variable) out to my named column (in this case, column G). the resulting selection would be from A5 to G5.

I've tried every combination I can think of, too many to list, plus searched the web, all to no avail. Any suggestions?
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub jgarland()
   Dim Fnd As Range
   
   Set Fnd = Range("1:1").Find("Ward", , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then Range(ActiveCell, Cells(ActiveCell.Row, Fnd.Column)).Select
End Sub
Change search term to suit.
 

jgarland

New Member
Joined
Nov 6, 2005
Messages
43
Hi, and thanks for responding.

My named column is "lastcolm". I replaced your code "Ward" with that name. Nothing was selected. Am I missing something?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
When you say a named column, is that a named range in the name manager, a range that has been declared in VBA, or a value somewhere in the sheet?
 

jgarland

New Member
Joined
Nov 6, 2005
Messages
43

ADVERTISEMENT

I selected an entire column and named it "lastcolm" (no quotes) via Excel's Name Manager.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
Ok, in that case, if you are always select a cell in col A, you can use
VBA Code:
   ActiveCell.Resize(, Range("lastcolumn").Column).Select
 

jgarland

New Member
Joined
Nov 6, 2005
Messages
43

ADVERTISEMENT

That works, but my active cell will not always be in Column A. It will vary depending on where in the code it is called. How do you code it if the active cell was in, for example, column D?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
   ActiveCell.Resize(, Range("Lastcolumn").Column - ActiveCell.Column + 1).Select
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,107
Messages
5,622,782
Members
415,927
Latest member
vedasinternational

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
Top