VBA Auto Filter with a Variable

KG Old Wolf

Board Regular
Joined
Sep 20, 2009
Messages
65
All,

I've read through the posts yet I cannot see what I am doing wrong....spent hours before posting.

I have a variable that I want to use in an auto filter. I've stripped the code to the barest minimum and still - no go. It is needed for use with a user form through wich the variable will be set by the ControlSource of a drop down list. But I can't get even the simplest code to work. All contributions are really appreciated. (The variable KG_Test is getting the proper number - I've also tried defining the variable with different data types (Long, Integer etc.)
'
'
Sub Human()
'
' Attempt to use a variable within an autofilter
'
KG_Test = Range("Input")
'
On Error Resume Next
ActiveSheet.ShowAllData
'
Range("D").Select
ActiveSheet.Range("db_Timesheet").AutoFilter Field:=4, _
Criteria1:="< & KG_Test", Operator:=xlAnd
Range("A1").Select
End Sub
'
'
Thanks,
KG
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Did you try?
Code:
Criteria1:="<" & KG_Test
It appears to work in my test

lenze
 
Upvote 0
Hi Lenze,

It does work and I really do appreciate your quick response. I, like so many others here, and entirely self-taught. Unlike so many others here - I am sometimes as dumb as a bag of rocks.... this is one of those times. I thought the entire expression had to be enclosed in quotes, not just the static operators.

Thanks,
Ken
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

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