Help with Variables in array! Easy fix

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hey Guys,

I have the below. I just cant figure the syntax out yet. Seems like an easy fix. I just want it to take the value and -1.

VBA Code:
ActiveSheet.Range("$A$1:$BA$10000").AutoFilter Field:=2, Criteria1:=Array( _
         (Right(thisworkbook.worksheets("Variables").Range("A10").value, 2)-2), (Right(thisworkbook.worksheets("Variables").Range("A10").value, 2)-1), (right(thisworkbook.worksheets("Variables").Range("A10").value, 2)), Operator:=xlFilterValues


the range("a10") is equal to fyi
202104



so basically it should filter for 02/2021, 03/2021, 04/2021 in the array. Any help is apprecaited!

Jordan
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
btw im getting a
compile error argument in parmarray not named issue on the operator= part
 
Upvote 0
Your AutoFilter statement is too complex and thus difficult to troubleshoot. Try using some variables to help you understand what is going on. Example:

VBA Code:
    Dim DVal As String, MVal As String, YVal As String
    Dim Month1 As String, Month2 As String, Month3 As String
    Dim MArray As Variant
   
    DVal = Range("A10") 'assumption is "202104" string value
    YVal = Left(DVal, 4) 'year
    MVal = Right(DVal, 2) 'month
   
    Month1 = Right("0" & Val(MVal) - 2, 2) & "/" & YVal
    Month2 = Right("0" & Val(MVal) - 1, 2) & "/" & YVal
    Month3 = MVal & "/" & YVal
   
    MArray = Array(Month1, Month2, Month3)
    ActiveSheet.Range("$A$1:$BA$10000").AutoFilter Field:=2, Criteria1:=MArray, Operator:=xlFilterValues
 
Upvote 0
Solution
Your AutoFilter statement is too complex and thus difficult to troubleshoot. Try using some variables to help you understand what is going on. Example:

VBA Code:
    Dim DVal As String, MVal As String, YVal As String
    Dim Month1 As String, Month2 As String, Month3 As String
    Dim MArray As Variant
  
    DVal = Range("A10") 'assumption is "202104" string value
    YVal = Left(DVal, 4) 'year
    MVal = Right(DVal, 2) 'month
  
    Month1 = Right("0" & Val(MVal) - 2, 2) & "/" & YVal
    Month2 = Right("0" & Val(MVal) - 1, 2) & "/" & YVal
    Month3 = MVal & "/" & YVal
  
    MArray = Array(Month1, Month2, Month3)
    ActiveSheet.Range("$A$1:$BA$10000").AutoFilter Field:=2, Criteria1:=MArray, Operator:=xlFilterValues
This worked great thank you so much!

Jordan
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,330
Members
449,155
Latest member
ravioli44

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