OFFSETting non-contiguous un named ranges.

rtirado

New Member
Joined
Jun 24, 2016
Messages
12
Hello,
I am attempting to offset a range. Because the range is non-contiguous the code I have come up with will not work.

Private Sub CommandButton1_Click()
Range(Selection.Offset(0, 3), Selection.Offset(0, -3)).Select
End Sub

When my range is selected and I hit the button instead of offsetting every cell highlighted the offset stops at the first non highlighted cell.

I have no experience with VBA except what I could find on-line.

I would greatly appreciate any suggestions.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hope this helps.

Code:
Private Sub CommandButton1_Click()
Dim rng As Range, c, nrng As Range
Set rng = Range(Selection, cells(Selection.Row, Columns.Count))
For Each c In rng
    If c.Interior.ColorIndex = xlNone Then
        Set nrng = Range(Selection, cells(c.Row, c.Column))
    Else
        Exit For
    End If
Next
 nrng.Select
 End Sub
 
Last edited:
Upvote 0
Code:
Dim area As Range, rng As Range
If Not Intersect(Selection, [A:C]) Is Nothing Then
    MsgBox "Do not select in columns A:C"
    Exit Sub
End If
For Each area In Selection.Areas
    If rng Is Nothing Then
        Set rng = Range(area.Offset(0, 3), area.Offset(0, -3))
    Else
        Set rng = Union(rng, Range(area.Offset(0, 3), area.Offset(0, -3)))
    End If
Next
rng.Select
 
Upvote 0
Hello Takae. Thank you for you quick response. The code is doing the same thing mine was doing before its just grabbing every cell possible to the right of my range.
 
Upvote 0
Hello Footoo. Your code works perfect. Thank you so much. I would of never been able to figure that out within my time frame.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
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