I think it's something simple, but I'm not that smart in VBA terms

gsimons85

New Member
Joined
Apr 1, 2014
Messages
32
Hello again

This is my second post trying to resolve this issue, hopefully I explain it better this time and can get some help.

I'm trying to filter on certain criteria for my pivot table and want to reference a particular set of cells.

Here is the code I got when I recorded my macro and manually filter:

Range("C1").Select
ActiveSheet.Range("$C$1:$D$28").AutoFilter Field:=1, Criteria1:=Array("1", _
"3", "YTD", "Z56"), Operator:=xlFilterValues


Instead of referencing "1" or "3", I'd like to reference the value in Cell A2 and A3 so that I can change the cell value and the pivot picks up the new value.

Ref CellsWeekDollarsState
1110Tx
3110Tx
Z56215Tx
YTD215Tx
320Tx
320Ca
425Ca
425Ca
YTD30Ca
YTD30Co
Z56100Co
Z56100Co

<TBODY>
</TBODY>


I'll keep working on it, but if someone could help point me in the right direction I'd be a very happy camper.

Thank you
Greg
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Not sure if this will work but try:
Rich (BB code):
Range("C1").Select
ActiveSheet.Range("$C$1:$D$28").AutoFilter Field:=1, Criteria1:=Array(Range("A1").Value, _
Range("A2").Value, Range("A3").Value, Range("A4").Value), Operator:=xlFilterValues
 
Upvote 0
Not sure if this will work but try:
Rich (BB code):
Range("C1").Select
ActiveSheet.Range("$C$1:$D$28").AutoFilter Field:=1, Criteria1:=Array(Range("A1").Value, _
Range("A2").Value, Range("A3").Value, Range("A4").Value), Operator:=xlFilterValues


When I enter this code, only Z56 and YTD get filtered, but the first two numbers are not included...
 
Upvote 0
Made a minor change and this now works for me, change in red:
Rich (BB code):
Range("C1").Select   '<--- you can delete this line, it's doing nothing
ActiveSheet.Range("$C$1:$D$28").Autofilter Field:=1, Criteria1:=Array(CST(Range("A2").Value), _
  CST(Range("A3").Value), Range("A4").Value, Range("A5").Value), Operator:=xlFilterValues

 
Upvote 0
Made a minor change and this now works for me, change in red:
Rich (BB code):
Range("C1").Select   '<--- you can delete this line, it's doing nothing
ActiveSheet.Range("$C$1:$D$28").Autofilter Field:=1, Criteria1:=Array(CST(Range("A2").Value), _
  CST(Range("A3").Value), Range("A4").Value, Range("A5").Value), Operator:=xlFilterValues



ActiveSheet.Range("$C$1:$D$28").AutoFilter Field:=1, Criteria1:=Array(CST(Range("A2").Value), _
CST(Range("A3").Value), Range("A4").Value, Range("A5").Value), Operator:=xlFilterValues

When I enter this code, I get a "COMPILE ERROR:SUB OR FUNCTION NOT DEFINED"

I figured out that when I change the numbers to Text format in the cells, it picks up with the original formula you provided...but I'd like to be able to pick up those cell values without converting to Text if possible....

Thanks for your help!
 
Upvote 0
I think I see what I was missing...I used CSTr instead of CST and it worked!!

ActiveSheet.Range("$C$1:$D$28").AutoFilter Field:=1, Criteria1:=Array(CStr(Range("A2").Value), _
CStr(Range("A3").Value), Range("A4").Value, Range("A5").Value), Operator:=xlFilterValues

Thank you very much for your help and assistance! :cool:
 
Upvote 0

Forum statistics

Threads
1,217,363
Messages
6,136,108
Members
449,993
Latest member
Sphere2215

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