![]() |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
Nate,
Two points You method doesn't allow an operator, that is presumably why the OP opted for Evaluate. It is 10 times quicker to use a simple If test. In fact, even allowing for an operator like so n = Timer For i = 1 To 1000000 mNum = 4 Select Case opStr Case "<": j = mNum < 4 Case "": j = mNum 4 Case "<": j = mNum < 4 Case "=": j = mNum = 4 End Select Next i Debug.Print "SimpleVB=" & Timer - n is twice as quick as yours. -- HTH RP (remove nothere from the email address if mailing direct) "Nate Oliver" wrote in message ... 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 |
Logical String Evaluation
Bob,
Two points: 1) "=" is the operator in my sub. 2) We're clearly working with different assumptions. You're assuming mNum is passed to the routine as Long Integer, if it is then I agree, drop all of this monkey business, it's a no brainer. I assumed mNum is passed to the Sub as a String, in which case, parse it the way most experienced VB[A] developers would. Regards, Nate Oliver |
Logical String Evaluation
"Nate Oliver" wrote in message ... Bob, Two points: 1) "=" is the operator in my sub. But that is not a variable as it is part of the formula. The only way you can make it variable without code change is using Evaluate! 2) We're clearly working with different assumptions. You're assuming mNum is passed to the routine as Long Integer, if it is then I agree, drop all of this monkey business, it's a no brainer. No I am assuming that Month is a variable, because we were told it is so, so we don't need a string assumption, in fact assuming something when told otherwise is perveres. I assumed mNum is passed to the Sub as a String, in which case, parse it the way most experienced VB[A] developers would. Oooh! |
Logical String Evaluation
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 |
Logical String Evaluation
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 |
Logical String Evaluation
Bob,
I did get a little of track there, sorry, wasn't sure why we were building strings and concatenating, etc... Looks like the Select Case procedure you posted does just fine. Regards, NPO |
Logical String Evaluation
This is reminiscent of yesterday when (in the VB6 forum) somebody said, "only
REAL programmers use zero-based arrays." Like we're not programmers if we choose to start counting at one. ;) I assumed mNum is passed to the Sub as a String, in which case, parse it the way most experienced VB[A] developers would. Oooh! |
Logical String Evaluation
Well, that's going to be tricky if you coerce a range to an array, and it
doesn't make one fake! And, it was qualified with the word 'most'. So, please don't be too quick to put words in my mouth. And, I was on the wrong track, which didn't help much, there's nothing to parse. Regards, NPO "Charlie" wrote: This is reminiscent of yesterday when (in the VB6 forum) somebody said, "only REAL programmers use zero-based arrays." Like we're not programmers if we choose to start counting at one. ;) I assumed mNum is passed to the Sub as a String, in which case, parse it the way most experienced VB[A] developers would. Oooh! |
Logical String Evaluation
Hello!! I wasn't talking about YOU, I was talking about somebody else.
"Nate Oliver" wrote: And, it was qualified with the word 'most'. So, please don't be too quick to put words in my mouth. |
Logical String Evaluation
What me? I didn't say '...the way most experienced VB[A] developers would'
"Charlie" wrote in message ... Hello!! I wasn't talking about YOU, I was talking about somebody else. "Nate Oliver" wrote: And, it was qualified with the word 'most'. So, please don't be too quick to put words in my mouth. |
Logical String Evaluation
Nate,
Yeah shame it derailed a little, as I thought that your point on time and performance was a good, valid point. I confused it a little by re-iterating what I had said to the OP earlier, that as far as I could see, all he wanted was If Month = 4 Then without using Month as a variable name of course :-). That will teach me to try and make two points at once :-). No harm done though. Regards Bob -- HTH RP (remove nothere from the email address if mailing direct) "Nate Oliver" wrote in message ... Bob, I did get a little of track there, sorry, wasn't sure why we were building strings and concatenating, etc... Looks like the Select Case procedure you posted does just fine. Regards, NPO |
Logical String Evaluation
No, sorry, I meant that someone else yesterday said, "only REAL
programmers...", and Nate's comment REMINDED me of that. But I wasn't putting words into his mouth. Oh well, I guess I shouldn't have jumped in! :) "Bob Phillips" wrote: What me? I didn't say '...the way most experienced VB[A] developers would' "Charlie" wrote in message ... Hello!! I wasn't talking about YOU, I was talking about somebody else. "Nate Oliver" wrote: And, it was qualified with the word 'most'. So, please don't be too quick to put words in my mouth. |
Logical String Evaluation
Whew! I thought you had jumped in to support me, then I got confused <ebg
Regards Bob "Charlie" wrote in message ... No, sorry, I meant that someone else yesterday said, "only REAL programmers...", and Nate's comment REMINDED me of that. But I wasn't putting words into his mouth. Oh well, I guess I shouldn't have jumped in! :) "Bob Phillips" wrote: What me? I didn't say '...the way most experienced VB[A] developers would' "Charlie" wrote in message ... Hello!! I wasn't talking about YOU, I was talking about somebody else. "Nate Oliver" wrote: And, it was qualified with the word 'most'. So, please don't be too quick to put words in my mouth. |
Logical String Evaluation
Tom,
You're right, that isn't what I literally asked. I thought my final statement implied a replacement of the 'Month' variable prior to evaluation. I didn't mean to mislead. Sorry for the confusion. Regards, VBA Dabbler "Tom Ogilvy" wrote: 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 |
All times are GMT +1. The time now is 01:15 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com