Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Does anyone know how to evaluate a logical string in VBA? An example of one
is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() If Month = 4 Then ... whatever -- HTH RP (remove nothere from the email address if mailing direct) "VBA Dabbler" wrote in message ... Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Do you mean, simply:
If Month = 4 Then ... ... End If "VBA Dabbler" wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try:
Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I think you mean
Application.Evaluate(month = 4) as month is a variable, which seems a pointless use of Evaluate to me when you can do an If -- HTH RP (remove nothere from the email address if mailing direct) "Mark Bigelow" wrote in message oups.com... Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
No, I want to evaluate the string as though it were 'code', and the string
can change for subsequent evaluations. The example I gave, however, would error on 'Type mismatch'. Thanks, VBA Dabbler "Bob Phillips" wrote: I think you mean Application.Evaluate(month = 4) as month is a variable, which seems a pointless use of Evaluate to me when you can do an If -- HTH RP (remove nothere from the email address if mailing direct) "Mark Bigelow" wrote in message oups.com... Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I was responding to Mark.
-- HTH RP (remove nothere from the email address if mailing direct) "VBA Dabbler" wrote in message ... No, I want to evaluate the string as though it were 'code', and the string can change for subsequent evaluations. The example I gave, however, would error on 'Type mismatch'. Thanks, VBA Dabbler "Bob Phillips" wrote: I think you mean Application.Evaluate(month = 4) as month is a variable, which seems a pointless use of Evaluate to me when you can do an If -- HTH RP (remove nothere from the email address if mailing direct) "Mark Bigelow" wrote in message oups.com... Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sub Tester1()
Dim m As Long m = 4 Dim sStr As String sStr = "m=4" Debug.Print Evaluate(sStr) End Sub returns Error 2009 If that is what you were suggesting. the evaluate command has no knowledge of VBA variables. It is a virtual worksheet cell. -- Regards, Tom Ogilvy "Mark Bigelow" wrote in message oups.com... Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You're right. However, the string would be concatenated with literals for
comparison. Hence, if m=4, as you have it, the concatenated string to be evaluated would be "4=4". "Tom Ogilvy" wrote: Sub Tester1() Dim m As Long m = 4 Dim sStr As String sStr = "m=4" Debug.Print Evaluate(sStr) End Sub returns Error 2009 If that is what you were suggesting. the evaluate command has no knowledge of VBA variables. It is a virtual worksheet cell. -- Regards, Tom Ogilvy "Mark Bigelow" wrote in message oups.com... Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
An example of one
is: "Month=4" Again, that isn't the question you asked. -- Regards, Tom Ogilvy "VBA Dabbler" wrote in message ... You're right. However, the string would be concatenated with literals for comparison. Hence, if m=4, as you have it, the concatenated string to be evaluated would be "4=4". "Tom Ogilvy" wrote: Sub Tester1() Dim m As Long m = 4 Dim sStr As String sStr = "m=4" Debug.Print Evaluate(sStr) End Sub returns Error 2009 If that is what you were suggesting. the evaluate command has no knowledge of VBA variables. It is a virtual worksheet cell. -- Regards, Tom Ogilvy "Mark Bigelow" wrote in message oups.com... Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks for pointing me back to the evaluate method - I had tried it
unsuccessfully and started looking elsewhere. I did get it to work. Thanks, VBA Dabbler "Mark Bigelow" wrote: Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
How about showing us what works.
-- Regards, Tom Ogilvy "VBA Dabbler" wrote in message ... Thanks for pointing me back to the evaluate method - I had tried it unsuccessfully and started looking elsewhere. I did get it to work. Thanks, VBA Dabbler "Mark Bigelow" wrote: Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Certainly. Try this.
MonthNumber = 4 OperatorString = "=" CriteriaValue = 4 If Evaluate(MonthNumber & OperatorString & CriteriaValue) Then Msgbox "It Works!" End If "Tom Ogilvy" wrote: How about showing us what works. -- Regards, Tom Ogilvy "VBA Dabbler" wrote in message ... Thanks for pointing me back to the evaluate method - I had tried it unsuccessfully and started looking elsewhere. I did get it to work. Thanks, VBA Dabbler "Mark Bigelow" wrote: Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#14
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello,
I tried it and something else: Sub OddStuff() Dim mNum As Long, opStr As String, myCrit As Long mNum = 4 opStr = "=" myCrit = 4 If Evaluate(mNum & opStr & myCrit) Then _ MsgBox "It Works! But not very well!" End Sub Sub ConventionalVB() Dim mNum As String Const MY_CRIT As Long = 4 Let mNum = "Month=4" If Val(Mid$(mNum, InStr(1, mNum, "=", vbBinaryCompare) + 1, Len(mNum))) _ = MY_CRIT Then MsgBox "It Works!" End Sub Looks like OddStuff() is about ~450% slower on a single call and ~1,050% slower on ten thousand calls, never mind the fact that it won't work in a non-Excel environment. If I was trying to parse a string, I might just do that. Regards, Nate Oliver |
#15
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
That's fine, but that isn't what you originally asked.
your code does Evaluate("4=4") But thanks for clearing it up. -- Regards, Tom Ogilvy "VBA Dabbler" wrote in message ... Certainly. Try this. MonthNumber = 4 OperatorString = "=" CriteriaValue = 4 If Evaluate(MonthNumber & OperatorString & CriteriaValue) Then Msgbox "It Works!" End If "Tom Ogilvy" wrote: How about showing us what works. -- Regards, Tom Ogilvy "VBA Dabbler" wrote in message ... Thanks for pointing me back to the evaluate method - I had tried it unsuccessfully and started looking elsewhere. I did get it to work. Thanks, VBA Dabbler "Mark Bigelow" wrote: Try: Application.Evaluate("Month=4") Mark VBA Dabbler wrote: Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#16
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
BTW, not a good idea to use Month as a variable name as that takes out the
VBA Month function. -- HTH RP (remove nothere from the email address if mailing direct) "VBA Dabbler" wrote in message ... Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#17
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Agreed.
"Bob Phillips" wrote: BTW, not a good idea to use Month as a variable name as that takes out the VBA Month function. -- HTH RP (remove nothere from the email address if mailing direct) "VBA Dabbler" wrote in message ... Does anyone know how to evaluate a logical string in VBA? An example of one is: "Month=4" If the variable 'Month' is 4, then the string would be true. Thanks, VBA Dabbler |
#18
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Option Explicit
Sub a() Dim m As String m = "1+2=3" Debug.Print Evaluate(m) m = "1+2=4" Debug.Print Evaluate(m) End Sub will result in (press CTRL + g) True False Avoid VBA keywords as variable names... (I took m, not Month :-) HTH, Bernd |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Formula evaluation | New Users to Excel | |||
Excel interprets Text String as value in logical Argument | New Users to Excel | |||
Multiple Logical Conditions With Date and String Comparison Not wo | Excel Worksheet Functions | |||
logical test and concatenate(string) | Excel Discussion (Misc queries) | |||
Evaluation Sheet | Excel Worksheet Functions |