Using string as if statement

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
So I draw a mistype error for this code when I try to build a if statement from a string. Is this even possible?

Code:
Dim filter as string

filter= "variable=5"

if filter then

blah

end if
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Looks like you are trying to test True / False

if so, then maybe like this

VBA Code:
Dim filter As Boolean
    Dim Variable As Long
   
    Variable = 5
   
    filter = CBool(Variable = 5)
   
    If filter Then
   
' do stuff
   
    End If

Dave
 
Upvote 0
Looks like you are trying to test True / False

if so, then maybe like this

VBA Code:
Dim filter As Boolean
    Dim Variable As Long
  
    Variable = 5
  
    filter = CBool(Variable = 5)
  
    If filter Then
  
' do stuff
  
    End If

Dave
Hey!

So I actuallly use a pretty long filter with a bunch of conditions in there which is why I built it as a string. Is there a way to make a string work as the if statement arguement like I posed?
 
Upvote 0
maybe

VBA Code:
filter = Variable
    
    If filter = 5 Then
    
    
    End If

If not what you want then publish all your code with explanation of what you are trying to Achieve - plenty here to assist

Dave
 
Upvote 0
Something like this?

VBA Code:
Sub test1()
Dim variable As Long

    variable = 5
    
    If Evaluate(variable & "=5") Then
        Debug.Print "It's true"
    End If
    
End Sub

Although I've got to think that there's a better way. If you post your code, we can offer some better suggestions.
 
Upvote 0
I'm not sure that can be done, however I would advise against using filter as your variable name, because Filter is a VBA reserved word.
 
Upvote 0
I suppose you could do something like this, dim your variable with the quantity of conditions minus 1.

VBA Code:
Sub test()
Dim t(0 To 1) As Boolean

variable = 5
variable2 = 3

t(0) = (variable = 5)
t(1) = (variable2 = 3)

If Application.WorksheetFunction.And(t) Then
    MsgBox "Both Values are correct"
Else
    MsgBox "Both values aren't correct"
End If

End Sub
 
Upvote 0
Something like this?

VBA Code:
Sub test1()
Dim variable As Long

    variable = 5
   
    If Evaluate(variable & "=5") Then
        Debug.Print "It's true"
    End If
   
End Sub

Although I've got to think that there's a better way. If you post your code, we can offer some better suggestions.
Interesting I have never used the evaluate function. I will see if that works. As others have posted this is not what I prefered to do but unfortunately VBA ADO filter method is very limited so using a IF statement seemed to be the best.
 
Upvote 0
Something like this?

VBA Code:
Sub test1()
Dim variable As Long

    variable = 5
   
    If Evaluate(variable & "=5") Then
        Debug.Print "It's true"
    End If
   
End Sub

Although I've got to think that there's a better way. If you post your code, we can offer some better suggestions.

I tested your code but modified and I draw a mismatch error. I am basically building a string that can be used as the conditions of a if statement so that as I loop through a recordset if the values dont meet that condition my code will just skip over that recordset. THe issue is that recordsets do not allow for very good filtering such that use logic like (A OR B) AND (C OR D) . So my only method is to create an artificial filter using an IF statement with my arguements. However, I need to build the conditions as a string because I have multiple combo boxes and what not I use to create the filtering. In the end I am realistically going to try and recreate excels filter functions via vba and display a userform with the ability to filter each column dependently. I do not use excels sheet functions so I am doing this via strins and vba but this is one small concept i need to narrow down and get these strings to act as if statements.

Code:
Sub test1()
Dim variable As String

    variable = "5=5 And 6=6"
    
    If Evaluate(variable) Then
        MsgBox "It's true"
    Else
        MsgBox "false"
    End If
    
End Sub
 
Upvote 0
The EVALUATE basically expects a phrase formatted as a sheet function, and you're giving it VBA syntax. Try:

If Evaluate("AND(5=5,6=6)") Then

or

If Evaluate("(5=5)*(6=6)") = 1 Then
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,869
Members
449,130
Latest member
lolasmith

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