VBA Use active.row to build selection

Will85

Active Member
Joined
Apr 26, 2012
Messages
254
Office Version
  1. 365
Platform
  1. Windows
I need help building a simple macro to select a range of cells based on an active cell.

The columns are predefined, but the row is variable.

Example

The active cell is A1, I want a macro that will extrapolate that row number and use it to select cells E1:H1
The active cell is A2, select E2:H2

The active cell will only ever be in column A
 
Not possible to keep the offset and resize language, so as not to hard code the columns?

It certainly is possible to do that but my only concern is that it might produce inconsistent results if something in column A is not selected first. If you are the only user of this macro, then please by all means have at it but I would not trust a perfect outcome each time if other people will be using this macro.

VBA Code:
Sub Select_cells_v2()
Dim rng As Range
Set rng = ActiveCell
rng.Offset(, 4).Resize(, 4).Select
End Sub

To clarify further: If other people will be using this macro, you can't always guarantee that they'll have something in column A selected first. Humans being humans, some user errors are bound to occur. This is the reason why I hard coded column E-H in my previous version.
 
Last edited:
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You would have to specify the columns you want to select in your code like this:

VBA Code:
Sub Select_cells_v2()
Dim r As Long
r = ActiveCell.Row
        Range("E" & r & ":H" & r).Select
End Sub
Now I am trying to select multiple ranges

Range("D" & r & ":H" & r, "J" & r & ":J" & r, "N" & r & ":P" & r).Select


Assuming r is 5
Trying to get it to Select D5:H5, J5, and N5:P5 at the same time, but its not working. Thoughts?
 
Upvote 0
Now I am trying to select multiple ranges

Range("D" & r & ":H" & r, "J" & r & ":J" & r, "N" & r & ":P" & r).Select


Assuming r is 5
Trying to get it to Select D5:H5, J5, and N5:P5 at the same time, but its not working. Thoughts?

You can use Union to select non-contiguous cells.
VBA Code:
Sub Select_cells_v3()
Dim r As Long
Dim rng As Range
r = ActiveCell.Row
    With ActiveSheet
        Set rng = Union(.Range("D" & r & ":H" & r), .Range("J" & r), .Range("N" & r & ":P" & r))
        rng.Select
    End With
End Sub
 
Upvote 0
Now I am trying to select multiple ranges

Range("D" & r & ":H" & r, "J" & r & ":J" & r, "N" & r & ":P" & r).Select
Try:
VBA Code:
Range("D" & r & ":H" & r & ",J" & r & ",N" & r & ":P" & r).Select
 
Upvote 0
You can use Union to select non-contiguous cells.
VBA Code:
Sub Select_cells_v3()
Dim r As Long
Dim rng As Range
r = ActiveCell.Row
    With ActiveSheet
        Set rng = Union(.Range("D" & r & ":H" & r), .Range("J" & r), .Range("N" & r & ":P" & r))
        rng.Select
    End With
End Sub
Thank you for all your help
 
Upvote 0
You're welcome, glad we could help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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