Selecting specific range in VBA

rbegoon94

New Member
Joined
Jun 8, 2016
Messages
3
I'm trying to select a range of cells based on the condition in a preceeding column.
For example if [L12] = 1 then select cells [N12:Q12]. This initial part I have no problem with but looping through a range of L cells raises I'm having trouble with. This is my current code

Code:
Dim k as integer

For k = 12 To 19
If Cells(k, "L").Value = 1 Then
    Range(Cells(k, "N"), Cells(14, "Q")).Select
    End If
Next k

The problem I'm having now is that it correctly identifies the 1s in column L but when it comes to a row with a one after a row with a 0 to select it also selects all of the intermediate cells as well.
the code Basically does a SHIFT+click when i want a CTRL+Click

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
didn't check this...hope it works

Sub macro()

Dim k As Integer
Dim rng As Range


For k = 12 To 19

If Cells(k, "L").Value = 1 Then
Set rng = Union(rng, Range(Cells(k, "N"), Cells(k, "Q")))
rng.Select
End If
Next k

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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