Variable Range for Array

Abgar

Active Member
Joined
Jun 20, 2009
Messages
265
Hi All,

Hoping you can help please. I have the below code, which use an array of data to autofilter another range of data:
Code:
Sub CodeSample()


    Worksheets("Pending Raw").Select


    Dim range1 As Range
    Dim var1 As Variant
    Dim sArray() As String
    Dim i As Long
     
    Set range1 = Sheets("Array Data").Range("A3:A13")
    var1 = range1.Value
    
     
    ReDim sArray(1 To UBound(var1))  '// Set the Array for departments
    For i = 1 To (UBound(var1))
        sArray(i) = var1(i, 1)
    Next


    ActiveSheet.Range("A1").AutoFilter Field:=4, Criteria1:=sArray, Operator:=xlFilterValues

So basically, i have a worksheet with a lot of data (Sheetname "Pending Raw"), which is using an autofilter to capture a list of items in the "Array Data" Worksheet, and then autofilter the "Pending Raw" data accordingly.

This works perfectly. However, im trying to make the array variable, to capture anything entered in column A (i.e. not just A3-A13).
I've tried changing the range line to this:
Code:
Set range1 = Sheets("Array Data").Range("A3:A" & Rows.Count).End(xlUp).CurrentRegion
but it pops a "Type Mismatch" error on ReDim sArray(1 To UBound(var1)).

Can anyone help me here please?

Cheers,
ABGar
 

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
Try this

Code:
Set range1 = Sheets("Array Data").Range("A3:A" & Sheets("Array Data").Range("A" & Rows.Count).End(xlUp).Row)
 
Upvote 0
I thought it would be something simple. Thanks Dante :)

Could be

Code:
Set range1 = Sheets("Array Data").Range("A3").CurrentRegion

But if you have values ​​above A3 or to the right of A3 they are also included in CurrentRegion.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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