Create an array based on cell values

deadlyliquidx

New Member
Joined
Feb 6, 2015
Messages
27
Hello friends!

I need to build an array based on a list in range A1:a300 on a sheet for a big code.
Code:
 sc.VisibleSlicerItemsList = Array(ActiveSheet.Range("a1"), ActiveSheet.Range("a2:a2"), ActiveSheet.Range("a3:a3"))
As you can see, I will manually have to put in range a1,a2,a3,a4 all the way up to 300 to build this array.
is there anyway this array can go through the column and build itself, like
sc.VisibleSlicerItemsList = Array(ActiveSheet.Range("a1:a300") which doesn't work when I tried it.
Better yet, would be if you can ask the array to stop building when there is a blank or error.

seems like a simple request but I cant figure it out for the life of me.
Let me know if you guys have any ideas.

Thanks!!!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This stops at the first error or blank cell.

Code:
    [COLOR=darkblue]Dim[/COLOR] arr1 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR], arr2 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR], i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], j [COLOR=darkblue]As[/COLOR] Long
    
    arr1 = Range("A1:A300").Value
    [COLOR=darkblue]ReDim[/COLOR] arr2(1 [COLOR=darkblue]To[/COLOR] 1)
    
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](arr1, 1)
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] IsError(arr1(i, 1)) [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] arr1(i, 1) <> "" [COLOR=darkblue]Then[/COLOR]
                j = j + 1
                [COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] arr2(1 [COLOR=darkblue]To[/COLOR] j)
                arr2(j) = arr1(i, 1)
            Else: [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        Else: [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] i
    
    [COLOR=darkblue]If[/COLOR] j > 0 [COLOR=darkblue]Then[/COLOR] sc.VisibleSlicerItemsList = arr2
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,015
Members
449,060
Latest member
LinusJE

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