Excel VBScript: String issue - excel

After entering any characters, the value which is stored in variable A is "0". Can anyone please assist me where I am going wrong as it is working fine if I enter number
Rookie
Public Sub MyFirstProgram()
Dim A As String
A = Val(InputBox("Enter your name", "NAME"))
MsgBox "My name is " & A
End Sub

The Val function converts a string to a Double numeric type.
Presumably, the names you are entering cannot be converted to a valid number, so the result is 0.
http://office.microsoft.com/en-us/excel-help/HV080557263.aspx
The Val function stops reading the string at the first character it can't recognize as part of a number.
So if you do something like =Val("123steve") it will return the numeric component: 123, but if you do =Val("Ebeneezer Scrooge") it stops, per the above remark -- since no characters have been converted to numeric value, it returns 0.

Related

Application.Check spelling not returning correct Boolean value

I am trying to prepare a excel file which automatically shows if any word is misspelled. There are codes that shows and highlights the particular word field when word is misspelled ,however I want to highlight a different cell if any
of the words are misspelled.
This is what I did:-
Function GFD() As Boolean
Application.CheckSpelling (Range("a1").Text)
End Function
'=======================================================
Function GFF() As Boolean
Application.CheckSpelling (Range("a2").Text)
End Function
Problem is in the function I am not getting correct value, if I write "asdfsd" in a2 the function is set as False ,which is correct. But if I write "perfect" which is correct, it again returns False.
Please help.
You have to set the function to the value returned by the application:
Function GFD() As Boolean
GFD = Application.CheckSpelling(Range("a1").Text)
End Function

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.

String Pattern Matching/Finding/Counting/Replacing

So this is a robust problem. I have a function which accepts 2 args (string_name, macros). Here it is so I can further explain.
function ParseStrings(string_name, macros)
return my_table[string_name]
-- All it does it returns the string_name's value
end
The problem is that the second arg is a table, and if it's a table then in the string there are going to be various parts that have the format "String stuff $MACRO_KEY; more string text" and the content between the $ and ; is the key to look up in the macro table sent with it. Now anytime a value like that appears in the string there will always be a second arg that's a table, so no problems their. I need to be able to count up the number of instances of macros in a string and then replace each macro component with it's respective macros' table value. So here's how the func is called in this instance...
local my_table = {
my_string = "My string content $MACRO_COMPONENT; more string stuff $MACRO_COMPONENT_SUB;$MACRO_COMPONENT_ALT;"
}
local macro = {
MACRO_COMPONENT = "F",
MACRO_COMPONENT_SUB = "Random Text",
MACRO_COMPONENT_ALT = "14598"
}
function ParseStrings(string_name, macros)
return my_table[string_name]
-- All it does it returns the string_name's value
end
ParseStrings("my_string", macro)
So I am thinking:
string.gsub(my_table[my_string]:match("%b$;"):sub(2,my_table[my_string]:match("%b$;"):len() - 1)
but this is a long and overtly complex answer (AFAIK) and from my tests it only does 1 replacement (because the pattern is only found once) and that's doesn't work well if there are multiple instances in the string. So ideas?

How to output a string in a excel/google-spreadsheet function?

I'm using the if statement to check whether a cell is a number. If yes, just return the original number, else, return the string 'Not a number'. However, I got some troubles while I was outputting the string 'Not a number'. Following are the function I'm using, how could I modify it?
=if(isnumber(A1), A1, 'Not a number')
It works while A1 is a number but fails while it's not a number.
You are missing double quotes.
Try this
=if(isnumber(A1), A1,"Not a number")

Pass multiple arguments to Excel Macro from Access?

I have a sub (macro) in Excel that I want to be able to call from Access, which looks like this:
Sub myMacro(param1 as string, param2 as string)
...
End Sub
In Access, I have:
xlBook.Application.Run "myMacro", string1, string2
But I get the error :
Runtime Error 450:
Wrong number of arguments or invaluid property assignment
How do I pass multiple parameter to Excel?
As per #Remou's comment, I checked the variable types being passed.
In this case, string2 was a Variant that was supposed to hold a String of numbers, but when the string of numbers got stored in string2 (the Variant), it was converted to a number (stored within a variant [?]).
Nonetheless, stricter (and thus correct) type declarations solved the issue.

Resources