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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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