VBA: How to loop thru groupings of non-contiguous cells

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have several groups of non-contiguous cells. The first group starts in A2, the next A14, the next A17 and so on. You can see each of these groupings are not the same length. I would like to use VBA to copy each one of these groupings, but how do I loop between each grouping. I have my process for what comes after the copying, I just need to know how to copy each grouping.

VBA Code:
Sub CopyMe()
    Range("A2").CurrentRegion.Copy
End Sub

Also, can I return in a msgbox the number of groupings? In this case 4

Areas.xlsm
AB
21490
35833
43676
57050
63061
74394
82984
98263
108981
11
121490
135833
148263
158981
16
171490
185833
193676
207050
213061
224394
23
241490
255833
263676
273061
284394
292984
308263
318981
Sheet2
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about
VBA Code:
Sub FryGirl()
   Dim Rng As Range
   
   With Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlConstants)
      For Each Rng In .Areas
         Rng.Resize(, 2).Copy
      Next Rng
      MsgBox .Areas.Count
   End With
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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