Building an Array in VBA from range

johnbird1988

Board Regular
Joined
Oct 6, 2009
Messages
199
Hello,

I need some help with building an array for a auto filter. At the moment it is not filtering / handling the value correctly. My code I have at the moment is below:

Code:
Sub Test()
Dim nDates as Variant

Set rng = .Range("A2:B" & nRows)

nDates = Sheet1.Range("L2:M8").Value

    .AutoFilterMode = False

With rng
        .AutoFilter Field:=1, Criteria1:=nDates, Operator:=xlFilterValues
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With

    .AutoFilterMode = False


End Sub

The problem is because I think Excel handles the data in Sheet1.Range("L2:M8").Value differently. Column L contains a list of financial years like "2016/2017, 2017/2018) and Column M is whole years like (2015, 2016). If I manually add these values into an array like Array("2015", "2016", "2016/2017", "2017/2018") this work find. I was just wondering what I am doing wrong or if there is another way I should be building my array.

Thank for our help,

Cheers,

John
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
John


As it is the code is pulling both columns and putting them in a 2 dimensional array, for the filter you would need a 1 dimensional array.

Which values do you want in the array?

Is it the dates from column L or the years from column M?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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