IF statement involving string function

btyturtle

New Member
Joined
Sep 25, 2015
Messages
9
Hi,

Please help or advise. I am trying to write an IF statement that allow me to autofilter my data list. My code is to use string function to detect a certain text in a cell (D3), so basically if D3 contains "77" then filter with a condition, else filter with another condition. Below is how my code looks like:

Code:
If Mid("D3", 6, 2) = "77" Then
       Range("$D$3:D" & Cells.SpecialCells(xlCellTypeLastCell).Row).AutoFilter Field:=1, Criteria1:=">=5", _
                Operator:=xlAnd, Criteria2:="<=6"
    Else
        Range("$D$3:D" & Cells.SpecialCells(xlCellTypeLastCell).Row).AutoFilter Field:=1, Criteria1:=">=4", _
                Operator:=xlAnd, Criteria2:="<=5"
End If

The problem is it does not seem to do what I want, clearly when I tried the function in Excel, it will return 77 to me; so I am sure the function is correct. However, the macro refused to detect that, thus only run the else condition ALL the time.

First, I thought because of the cell format (because the cell has mixture of letters and numbers).

I added:

Range("A3").NumberFormat = "@"

before the IF statement starts.

But, it still doesn't work.

Any advice on how to work around this, otherwise what else could be a problem.

TO TRY: Here is my data list looks like (assume the first column and first row is D3
Text 77.00
3.80
40
4.35120
4.6123
4.63125
5130
5.4135
6200
6.1231

<tbody>
</tbody>
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Looks like the values in column D are NOT numeric (they appear left alligned), they're "Numbers stored as text"
Therefor the filter will not consider them between any 2 numbers.

What does this return
=ISNUMBER(D3) and filled down to end of data?
 
Upvote 0
Looks like the values in column D are NOT numeric (they appear left alligned), they're "Numbers stored as text"
Therefor the filter will not consider them between any 2 numbers.

What does this return
=ISNUMBER(D3) and filled down to end of data?


In my original file, D3 returns FALSE, and the rest of the data return TRUE.

To use the string function, what is the format supposed to be?

The content in D3 is a mixture of Text(space)## so, it should have General formatting.

However, one of the ways for me to have conditional autofilter is determined by what ## followed after the Text(space).

Thank you.
 
Upvote 0
Can you define "Doesn't work" ?

Do you get an error? What error? On which line?
Does nothing at all happen?
Are the values not filtered correctly? Which values DID or Did Not end up visible after the filter?
 
Upvote 0
Hi,

Please help or advise. I am trying to write an IF statement that allow me to autofilter my data list. My code is to use string function to detect a certain text in a cell (D3), so basically if D3 contains "77" then filter with a condition, else filter with another condition. Below is how my code looks like:

Code:
If Mid([B][COLOR="#FF0000"]Range([/COLOR][/B]"D3"[B][COLOR="#FF0000"])[/COLOR][/B], 6, 2) = "77" Then
       Range("$D$3:D" & Cells.SpecialCells(xlCellTypeLastCell).Row).AutoFilter Field:=1, Criteria1:=">=5", _
                Operator:=xlAnd, Criteria2:="<=6"
    Else
        Range("$D$3:D" & Cells.SpecialCells(xlCellTypeLastCell).Row).AutoFilter Field:=1, Criteria1:=">=4", _
                Operator:=xlAnd, Criteria2:="<=5"
End If
I think you need to include what I show in red to make the code line work correctly.
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

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