How to concatenate variable to Range Shortcut [ ]? - excel

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.

Related

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.

Merge string differences with Access SQL/VBA

I have two Fields, with partly different strings. FieldA:= "String1" FieldB:= "String1; String2" (So, the main difference between the two fields is the "; String2" in FieldB). The result i want to see is also "String1; String2", but the first half i want from FieldA, and the second half i want from FieldB. Is there any way using Access SQL/VBA function to solve this problem?
With the assumption that your values will always contain a semi-colon, you could also use the Split function in the following way:
[FieldA] & ";" & Split([FieldB],";")(1)
Yes. Use string manipulation functions. This is a relatively simple case for string manipulation, assuming the strings are consistent with the examples given. Consistency is critical to string manipulation. Assuming there is a space following the semi-colon, try:
[FieldA] & "; " & Mid([FieldB], InStr([FieldB], ";") + 2)
Expression can be used in query or textbox or VBA.
Suggest you do some research and learn about these and other string functions.

application.run with variables set within run method function

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

WorksheetFunction.value() missing in Excel

In a spreadsheet formula, =VALUE("$100") will evaluate to the numeric value of 100. I then tried to access this function in VBA via WorksheetFunction object, however it is missing.
In VBA I tried the conversion function Val("$100"), however that returns 0. So how can I accomplish this via VBA?
Val() only really works if the string is all numbers I'm afraid - currency signs cause it a problem.
If you're always going to have the same currency sign in the string, it might be worth using something like
StringName = replace(StringName, "$", "")
to take out the $ by replacing it with "" - otherwise if your strings aren't always going to be this predictable the below question might help:
How to find numbers from a string?
see https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.numbervalue
example of using above, which will return a value of -1234.56:
MsgBox WorksheetFunction.NumberValue("-$1,234.56", ".", ",")
Note that if the result is non-numeric, it throws an error. For example (swapping the comma grouping and decimal character params which is invalid in this case):
MsgBox WorksheetFunction.NumberValue("-$1,234.56", ".", ",")
I don't understand why the above link doesn't have any version info. It is currently dated 2019‎-‎05‎-‎23 - no idea if that's because it is new or if it was recently updated.

C# 4.0 function to check for first four characters in the string

I need to validate for valid code name.
So, my string can have values like below:
String test = "C000. ", "C010. ", "C020. ", "C030. ", "CA00. ","C0B0. ","C00C. "
So my function needs to validate below conditions:
It should start with C
After that next 3 characters should be numeric before .
Rest it can be anything.
So in above string values, only ["C000.", "C010.", "C020.", "C030."] are valid ones.
EDIT:
Below is the code I tried:
if (nameObject.Title.StartsWith(String.Format("^[C][0-9]{3}$",nameObject.Title)))
I'd suggest a regex, for example (written off the top of my head, may need work):
string s = "C030.";
Regex reg = new Regex("C[0-9]{3,3}\\.");
bool isMatch = reg.IsMatch(s);
This regex should do the trick:
Regex.IsMatch(input, #"C[0-9]{3}\..*")
Check out http://www.techotopia.com/index.php/Working_with_Strings_in_C_Sharp
for a quick tutorial on (among other things) individual access of string elements, so you can test each element for your criteria.
If you think your criteria may change, using regular expressions gives you maximum flexibility (but is more runtime intensive than regular string-element evaluation). In your case, it may be overkill, IMHO.

Resources