Sorting with a variable

hyoung3

New Member
Joined
May 9, 2011
Messages
21
I am having difficulty figuring out how to sort with a variable. I have a macro that used a given cell value as a variable. I have named this variable "a". I have checked to make sure the variable is set to the value I want from the cell selected. I want to now use this variable as a sorting criteria. Here is the code I used to try and do this.
ActiveSheet.Range("$A$1:$T$" & r).AutoFilter Field:=5, Criteria1:= _ "<" & a, Operator:=xlAnd
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Can you show the code that worked?

Then also the code you're trying to use, including the line that assigns a value to the variable a

And what type of data is your variable? Is it a date?
 
Upvote 0
A is a time variable and the value for this specific operation is 113100. The sorting work if "Criteria1:="<113300", but I want to substitute the 113300 with a because the time is constantly changing.

Dim a As Single
a = Cells(r - 1, 5).Value
ActiveSheet.Range("$A$1:$T$" & r).AutoFilter Field:=5, Criteria1:= _
"<" & a, Operator:=xlAnd
 
Upvote 0
Hi hyoung3,

It should work like this (without
_ after :=):
Code:
ActiveSheet.Range("$A$1:$T$" & r).AutoFilter Field:=5, Criteria1:= [COLOR=Red]"<" & a[/COLOR], Operator:=xlAnd
Or you can define a string variable as follow:
Code:
Var="<3"
or 
Var = "<" & a 
ActiveSheet.Range("$A$1:$T$" & r).AutoFilter Field:=5, Criteria1:=[COLOR=Red]Var[/COLOR], Operator:=xlAnd
Hope this helps,

Regards
 
Upvote 0
What exactly is in Cells(r - 1, 5) ?

is it a real time value like 11:33:00 ?
or just the number 113300 ?

Format that cell as a NUMBER with 6 decimals.
What does it show?
 
Upvote 0
It shows 113100. I got it to work. I really apologize for the confusion. I couldn't post the whole code because it is long. What happened was the code was working correctly, but I didn't know it because I was trying to filter and delete everything less than 113100. That was actually everything because there was not a time greater than that.

Thank you both for your help. I really appreciate it!!!
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,304
Members
452,904
Latest member
CodeMasterX

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