VBA - Find and Add cells to dynamic array

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
I have column C with the following structure:

[blank]
item1.1
item1.2
item1.3
BA1
[blank]
item2.1
item2.2
BA2
[blank]
item3.1
item3.2
item3.3
BA3
[blank]
and so on...

I am looking for a macro that finds let's say "BA2" and then adds all items above "BA2" (until a blank cell is reached) to a dynamic array. In this example the array will have item2.1 and item2.2

Not sure how to go about this, any help would be much appreciated. :)
Andrew
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
With the value to find in cell "B1", try this for result in column "C".
Code:
[COLOR="Navy"]Sub[/COLOR] MG22May27
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range, Ray [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Range("A:A").SpecialCells(xlCellTypeConstants).Areas
    [COLOR="Navy"]If[/COLOR] Dn(Dn.Count) = [B1] [COLOR="Navy"]Then[/COLOR] Ray = Application.Transpose(Dn)
[COLOR="Navy"]Next[/COLOR] Dn
'[COLOR="Green"][B]REMOVE LINE BELOW IF ARRAY lIST NOT REQUIRED[/B][/COLOR]
[COLOR="Navy"]If[/COLOR] IsArray(Ray) [COLOR="Navy"]Then[/COLOR]
    Range("C1").Resize(UBound(Ray)) = Application.Transpose(Ray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
With the value to find in cell "B1", try this for result in column "C".
Code:
[COLOR="Navy"]Sub[/COLOR] MG22May27
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range, Ray [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Range("A:A").SpecialCells(xlCellTypeConstants).Areas
    [COLOR="Navy"]If[/COLOR] Dn(Dn.Count) = [B1] [COLOR="Navy"]Then[/COLOR] Ray = Application.Transpose(Dn)
[COLOR="Navy"]Next[/COLOR] Dn
'[COLOR="Green"][B]REMOVE LINE BELOW IF ARRAY lIST NOT REQUIRED[/B][/COLOR]
[COLOR="Navy"]If[/COLOR] IsArray(Ray) [COLOR="Navy"]Then[/COLOR]
    Range("C1").Resize(UBound(Ray)) = Application.Transpose(Ray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
Regards Mick

thanks for your help Mick, BA01, BA02, BA03 are formula cells. So as the items above them.
Also why are you referring to columns A:A in your solution?
 
Upvote 0
Try changing that line to the below:-
Code:
For Each Dn In Range("A:A").SpecialCells(xlCellTypeFormulas).Areas
 
Upvote 0
Sorry missed column "C" , Try this for Value = "B1" and results in column "D".
Code:
[COLOR="Navy"]Sub[/COLOR] MG22May06
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range, Ray [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Range("C:C").SpecialCells(xlCellTypeFormulas).Areas
[COLOR="Navy"]If[/COLOR] Dn(Dn.Count) = [B1] [COLOR="Navy"]Then[/COLOR] Ray = Application.Transpose(Dn)
[COLOR="Navy"]Next[/COLOR] Dn
'[COLOR="Green"][B]REMOVE LINE BELOW IF ARRAY lIST NOT REQUIRED[/B][/COLOR]
[COLOR="Navy"]If[/COLOR] IsArray(Ray) [COLOR="Navy"]Then[/COLOR]
    Range("D:D").ClearContents
    Range("D1").Resize(UBound(Ray)) = Application.Transpose(Ray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,302
Members
449,218
Latest member
Excel Master

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