application.run with variables set within run method function - excel

I am trying to run a macro with variables as follows:
Range("A1").Application.Run (SetConditionalFormatingSub,ConditionB="=""O-BETTER-T-PRV""",ConditionW = "=""O-WORSE-T-PRV""",ConditionM = "=""O-MIXED-T-PRV""")
But I get an error as follows:
Compile error
Expected =
Still a novice in excel coding, can't figure out what seems to be the problem.
Hopefully you guys can help! Thanks in advance.

Try:
Range("A1").Application.Run "SetConditionalFormatingSub", "='O-BETTER-T-PRV'", "='O-WORSE-T-PRV'", "='O-MIXED-T-PRV'"
Notes:
don't use parentheses () when calling a sub;
you can't use names for sub parameters (like ConditionW) with Run method - it only accepts arguments by position (the same order they are defined in your macro);
you can't use quotes inside quotes. Instead, use single quotes, for example " 'Hello' ", instead of " "Hello" "
I don't think you need an Application.Run method for your task. You can set conditional formatting for your range directly using FormatConditions.Add method:
FormatConditions.Add Method

Related

How to concatenate variable to Range Shortcut [ ]?

I prefer to use Range Shortcut [ ] instead of Range(" ").
But I cannot concatenate variable to it.
ActiveSheet.Range("A2:A" & LastRow).Select ‘This works
ActiveSheet.[A2:A & LastRow].Select ‘not works
I got error Object required.
You cannot add variables within the brackets. That's why it's returning the object required error.
ExcelHero did tests and found that the brackets run slower than when range is clearly defined in the code:
http://www.excelhero.com/blog/2010/06/when-working-in-vba-we.html
Use Evaluate to do what you want to do
ActiveSheet.Evaluate("A2:A" & lastRow).Select
Using square brackets (for example, "[A1:C5]") is identical to calling
the Evaluate method with a string argument.
Further reading
refer-to-cells-by-using-shortcut-notation
excel.application.evaluate
PS As the OP is after having a string variable in the expression with [] and as I do not think that this is possible another quote from the documentation to support my point of view.
The advantage of using square brackets is that the code is shorter.
The advantage of using Evaluate is that the argument is a string, so
you can either construct the string in your code or use a Visual Basic
variable.

What is the difference between .Keys and .Keys() when looping through a Dictionary in VBA [duplicate]

Sub Main()
Console.WriteLine("check")
Console.Read()
End Sub
Why does Sub Main () need them? How do they apply to this procedure?
.WriteLine("") here i am adding a value.
Console.Read() is this holding the value "check" to show on console? Why why are they here.
I know all you experts think this is a dumb question, however i can not wrap my head around it help! To me these are boxes that hold or pass a procedure value.
Is sub main a container holding the code the uses enters? If so, why is it that when a form button is used it is full? But here a VB default unused and empty? Seems to me with no event values it should not be there....?????
Parenthesis are required when they are required, and optional when they are optional. In the case of empty parameter/argument lists parenthesis are "just for show".
A Sub Procedure can be declared as Sub Main() or Sub Main - the parenthesis are optional when there are no parameters. Likewise, procedures/functions can be invoked without parenthesis if (and only if) no arguments are supplied.
Sub A ' ok, no parameter list - no need for parenthesis
Sub A() ' it's fine to use parenthesis anyway
Sub B(x as Integer) ' need parenthesis for parameter list
obj.A ' ok, no arguments - no need for parenthesis
obj.A() ' it's fine to use parenthesis anyway
obj.B(42) ' need parenthesis when arguments are specified
In the above, the definitions of A and invocations of A are equivalent as the parenthesis are optional in these cases.
When calling a method you do have a choice in VB whether you want to include the parentheses if there are no parameters. The same holds true for the definition of a method, be it a function or a sub.
See http://msdn.microsoft.com/en-us/library/dz1z94ha.aspx (Sub Statement on MSDN).
Calling a Sub Procedure
You call a Sub procedure by using the procedure name in a statement
and then following that name with its argument list in parentheses.
You can omit the parentheses only if you don't supply any arguments.
However, your code is more readable if you always include the
parentheses.

How to express a Double in Shape.SetFormula?

I am trying to input a Double value into a Shape.SetFormula command
for example the following is something which I recorded via the journaling function of NX:
moveObjectBuilder1.TransformMotion.Angle.SetFormula("12.5")
According to the microsoft website, the value inside the () can only be an Integer, and obviously, any value (no matter Integer or Double value) written in this form "xxx" can be executed.
As far as I understood, "" is a String, so I changed the code like the following :
Function value() As String
Return 25/2
End Function
Sub Main(ByVal As String)
.
.
.
moveObjectBuilder1.TransformMotion.Angle.SetFormula(value())
.
.
.
End Sub
However, there will be a syntax error if the code is written like this.
May I ask, how can I let the Shape.Formula() command read a Double value? Or how can I let the Function return a value which will be in this format "..."?
Thank you very much!
I have no experience with that application but, as far as I can tell, the parameter for that method is type String because it accepts a String containing a mathematical formula that will be parsed internally. In that case, something like this should work:
moveObjectBuilder1.TransformMotion.Angle.SetFormula("25 / 2")
and do exactly the same thing as this:
moveObjectBuilder1.TransformMotion.Angle.SetFormula("12.5")
If that's not working for you then please explain EXACTLY what happens when you do it.
I've managed to solve my own problem. It was actually quite stupid, I am just simply not an expert in programming. It only took me several more tries to get the solution to my problem.
moveObjectBuilder1.TransformMotion.Angle.SetFormula("Sqrt(2)")
This is the correct way to express "Rotate this object with the angle of square root of 2."
Okay now let's talk about the problem which I've encountered. What I want is, import a value via a function, and then process it. So, SetFormula only accepts String. Therefore I can do the following :
Function value() As Double
Return 25/2
End Function
Sub Main(ByVal arg() As String)
.
.
.
moveObjectBuilder1.TransformMotion.Angle.SetFormula(value().ToString)
.
.
.
End Sub

How are the double-quotes managed when I put this code into a loop?

On SO, I was just given two answers that both work when called a single time. Now I want to put them in a loop and loop over several rows of data. However, I'm having a heck of time getting the code correct. I'm suspect it has to how I'm handling the double quotes.
The stand alone code lines are as follows.
Var = ActiveSheet.Evaluate("And(A1:F1)") and
Var = Application.WorksheetFunction.And(Range("A1:F1"))
for the first example I tried:
for i = 2 to 20
Var = ActiveSheet.Evaluate("And(A & i & :F & i)")
Next i
This produces "Error 2015"
for the second:
for i = 2 to 20
Var = Application.WorksheetFunction.And(Range("A" & i & ":F" & i))
Next i
This produces a line of red code
What am I doing wrong?
The Visual Basic Editor is making this harder than it should be, because its default syntax highlighting is making string literals the same color as identifiers:
You can change that under Tools/Options, and make Identifier Text a different color - here teal:
Now string literals are still black, but now identifiers look visually distinctive:
What you want to make sure, is that your variables are syntax-highlighted like identifiers - so they're teal, not black - like in your second example:
Contrast with your first attempt, where i doesn't get syntax-highlighted as the identifier it should be:
And since you know that i is a VBA variable and you want VBA to concatenate its value into this string, then i being syntax-highlighted as any other string literal (and not as an identifier) is your visual cue that something's off!
Compare to #JNevill's fixed version:
With Identifier Text having a different syntax highlighting than string literals in the editor, it becomes much easier to quickly locate a variable that's accidentally inside a string literal.
That first snippet isn't working, because ActiveSheet.Evaluate takes its parameter and gives it to Excel's expression evaluation engine, ...which has no idea what to do with this i. Variable i only exists in the execution context of the VBA code: only VBA code can evaluate its value.

Subsonic IN Query with Multiple Strings

I'm trying to do a Subsonic Query with an IN statement containing multiple strings. If I manually hard-code the strings, it works fine. Example:
Dim qry As New [Select]("mySelectColumn")
qry.From(table.Schema)
qry.Where(table.Columns.mycolumn).In("string1", "string2", "string3")
Hoever, I need to be able to pull the IN statement strings from a single variable in VB, which would make the last line look like:
qry.Where(table.Columns.mycolumn).In(combinedString)
But whenever I try and concat the strings together into a single VB string, I get no results. I can't even tell exactly what SQL it's trying to pass. Using buildsqlstatement() just shows the IN statement with :mycolumn0In1, :mycolumn0In2, :mycolumn0In3...I can't tell what it's actually trying to do.
I've tried these variations for the VB variable to no avail:
mystring = """mystring1"", ""mystring2"""
mystring = "'mystring1', 'mystring2'"
Any ideas how I can concat multiple strings into one IN clause with VB and Subsonic?
A coworker pointed me to http://blog.levo.us/index.php/2008/06/25/subsonic-select-where-in-solution/ and I eventually found a solution. My problem was that I was not explicitly declaring the VB variable as an arraylist. I was simply Dim'ing it and assigning it the results of a function which were returned as an ArrayList.
what didn't work - the getstrings() function returns an ArrayList:
Dim myarraylist = getstrings()
...
qry.Where(table.Columns.mycolumn).In(myarraylist)
What did work:
Dim myarraylist as ArrayList = getstrings()
...
qry.Where(table.Columns.mycolumn).In(myarraylist)

Resources