VBA/select contiguous cells as separate batches

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
393
Office Version
  1. 2021
Platform
  1. Windows
Hi

Say, A1:A6 are to be selected but with 2 cells as a group, i.e. A1:A2,A3:A4,A5:A6
I tried

VBA Code:
With ActiveSheet
Union( .Range("A1:A2"), .Range("A3:A4"), Range("A5:A6")).Select
End With

However, the result is A1:A6 being selected instead of 3 selection batches A1:A2, A3:A4 and A5:A6
The above is a simplified case of my scenario. I just wonder, if in general, there is a way for VBA to group selected contiguous cells by N number as a batch, and return contiguous selection as separate batches ( like what we select using Ctrl + MouseLeftClick on cells ).
 
You didn't really answer @Joe4's questions in post #4, it is not considered to be good practice to "select" objects in VBA so giving that as a goal doesn't really help.

Are you sure one of the 2 options below wouldn't work for you ?

VBA Code:
Sub RangeAreas_UsingArray()

    Dim rngFull As Range
    Dim rngPart As Range
    Dim sAddr As Variant
    Dim i As Long
   
    sAddr = Array("A1:A2", "A3:A4", "A5:A6")
   
    For i = 0 To UBound(sAddr)
        Debug.Print Range(sAddr(i)).Address, Range(sAddr(i)).Cells.Count
    Next i

End Sub


Sub RangeAreas_UsingString()

    Dim rngFull As Range
    Dim rngPart As Range
    Dim sAddr As Variant
    Dim i As Long
   
    sAddr = Split("A1:A2,A3:A4,A5:A6", ",")
   
    For i = 0 To UBound(sAddr)
        Debug.Print Range(sAddr(i)).Address, Range(sAddr(i)).Cells.Count
    Next i

End Sub
Sorry. This is a necessary step to use "Select" to continue and glue to the written codes later on.

I just wanna see if VBA is able to select contiguous cells in batches at a large scale. I know I can loop selected contiguous cells through(either forward of backward)and manipulate those batches alone by setting N = 2. However, it may not fit well into the codes under next level of tasks. And there will be a hard time to rewrite everything. :(
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Sorry. This is a necessary step to use "Select" to continue and glue to the written codes later on.

I just wanna see if VBA is able to select contiguous cells in batches at a large scale. I know I can loop selected contiguous cells through(either forward of backward)and manipulate those batches alone by setting N = 2. However, it may not fit well into the codes under next level of tasks. And there will be a hard time to rewrite everything. :(
I still think there is a good chance that you many not to select any of these ranges in order to work with them.
But if you don't show us what these "other codes" look like, there isn't any way for us to know for sure or provide you with alternatives.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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