Formula error due to different Languages in VBA Excel - excel

I have a vba code to fill dynamically conditionnal formatting. The problem is that my code works fine on english version of Excel, but if I send to someone with the french excel version, it's giving them an error.
I realized that the problem is that I put in my vba code a comma ',' in english version, but in french version it excpects a ";" separator to work.
How can I solve this so that my code works on any excel version ? I can't create an excel for each user, based on his local version.
To make things clear so that you may help :
here is the part of my code that is causing me the problem
sFormula is a string contaning :
sFormula=AND(OR($AP14="",$AR14=""),NOT($C14=""))
Range(sMyRange).FormatConditions.Add Type:=xlExpression, Formula1:=sFormula
In enlglish version of Excel All works fine
In french version I have to replace in my VBA code the ',' in sFormula by ';' to makes it work, otherwise I get a runtime error when the Macro code is executed
If I replace in my code the previous sFormula, by the following one (just changing the , by ;) it works fine on french version
sFormula=AND(OR($AP14="";$AR14="");NOT($C14=""))
Thanks in advance for your help.

I think you can write code by judging the language when installing Excel.
Dim sFormula As String
If Application.LanguageSettings.LanguageID(msoLanguageIDInstall) = msoLanguageIDEnglishAUS Then
sFormula = "=AND(OR($AP14="""",$AR14=""""),NOT($C14=""""))"
ElseIf Application.LanguageSettings.LanguageID(msoLanguageIDInstall) = msoLanguageIDFrench Then
sFormula = "=AND(OR($AP14="""",$AR14="""");NOT($C14=""""))"
End If
Range(sMyRange).FormatConditions.Add Type:=xlExpression, Formula1:=sFormula

Based on the comments of #Scott Craner , I solved my problem using the following functionThanks Again to Scott Craner and to #Dy.Lee for their help.
Here is my function so that it may help someone else :
Public Function GetLocalFormula(sformula As String, Optional sCell As String = "A1") As String
'This function will Convert and English formula to localized formula
'SCell is the temporary cell that we will use to convert the Formula, by default it's A1
Dim temporary As String
Dim result As String
With Range(sCell)
temporary = .formula
.formula = sformula
result = .FormulaLocal
.formula = temporary
GetLocalFormula = result
End With 'With Range(sCell)
End Function

Related

VBA - cycle through number formats

I have put together the following code so that when I repeatedly press a shortcut, the number in a cell automatically switches formats between:
2020
2020A
2020E
2020A/E
2,020%
2,020x
2,020bps
Here's the code that isn't currently working:
Sub Number_Format_Cycle()
Dim x, myFormats
myFormats = Array("###0;(###0);-", "###0A;(###0)A;-", "###0E;(###0E);-", "###0A/E;(###0A/E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0bps;(#,##0)bps;0bps")
x = Application.Match(ActiveCell.NumberFormat, myFormats, False)
If IsError(x) Then x = 0
Selection.NumberFormat = myFormats((x Mod (UBound(myFormats) + 1)))
End Sub
Any help would be much appreciated
Cheers,
Thomas
You have to use a backslash (\) in order to add character literals to make them showing up exactly as typed.
Changing the myFormats assignment to the following codeline worked for me:
myFormats = Array("###0;(###0);-", "###0\A;(###0)\A;-", "###0\E;(###0\E);-", "###0\A\/\E;(###0\A\/\E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0\bp\s;(#,##0)\bp\s;0\bp\s")
Re-Check each number format you intend to add after a test setting against the array elements.
Caveat: I have to admit that I can't explain the last \bp\s format, nevertheless it works.

Passing Array of strings to Sheets() in VBA

i intend to save the sheets as pdf which works fine, however when i wanted
to save sheets with names stored in some cell i run into a problem
i have written following function in VBA and i cant' figure out what is the problem or how to walk around it
Sub Export_to_pdf()
Dim SheetArr() As String
.
.
.
SheetArr = Split(Worksheets("EXPORT").Range("C12").Value, ",")
Sheets(SheetArr).Activate
.
.
.
End Sub
the problem with code above is that when i pass to Sheets regular array like Sheets(Array("sh1","sh2")).activate - its fine but when i use the split on cell which content looks like "sh1","sh2" it throws error
from what i tried to debug i found out that possibly the problem is that Split returns String array as opposed to variant array which i suspect to be the problem, how can i solve this problem ? casting string values with CVar doesn't seem to work
Thanks for any answers
This works fine for me:
Sub Export_to_pdf()
Dim SheetArr
SheetArr = Split(Sheet1.[A1], ",")
Sheets(SheetArr).Select '<< not Activate
End Sub
In A1: Sheet1,Sheet2

Trying to parse excel string

I am trying to parse a string from teamspeak. I am new to the functions of excel. I have accomplished this with php but I am driving myself nuts excel. This is the string I am trying to parse:
[URL=client://4792/noEto+VRGdhvT9/iV375Ck1ZIfo=~Rizz]Rizz[/URL]
This is what I have accomplished so far:
=TRIM(MID(B22, 15, FIND("=",B22,12) - FIND("//",B22)))
which returns
4792/noEto+VRGdhvT9/iV375Ck1ZIfo=~
I am trying to get it to return:
noEto+VRGdhvT9/iV375Ck1ZIfo=
Any suggestions? I am looked of splitting of strings and the phrasing is just really confusing. Any help would be appriciated.
Paste the URL in A3, then this formula in B3. You can adjust the cell references as needed. It's a lot of nested functions, but it works.
=left(right(A3, len(A3)-find("/",A3,find("//",A3,1)+2)),find("=",right(A3, len(A3)-find("/",A3,find("//",A3,1)+2)),1))
Or you can use a user-defined function in VBA:
Function RegexExtract(myRange As Range) As String
'VBA Editor, menu Tools - References, add reference to Microsoft VBScript Regular Expressions 5.5
Dim regex As New RegExp, allMatches As MatchCollection
With regex
.Global = True
.pattern = "\d+/(.+=)"
End With
Set allMatches = regex.Execute(myRange.Value)
With allMatches
If .Count = 1 Then
RegexExtract = .Item(0).SubMatches(0)
Else
RegexExtract = "N/A"
End If
End With
End Function
Then use it as formula:
=RegexExtract(A1)
I am trying to parse a string
For that:
=MID(A1,20,28)
works.
Now if you have more than one string maybe the others are not of an identical pattern, so the above might not work for them. But in that case if to help you we'd need to know something about the shape of the others wouldn't we.

Excel UDF not appearing in drop down menu

I wrote a User Defined Fucntion in Excel. It works great with no issues. I even wrote a description for it under the object properties menu.
The problem is, my UDF never shows up in the Excel drop down menu that appears when I start to type a function. I want the user to be able to see my UDF, named removeNumbers, when they go into a cell and start to type out a function.
I would also like them to be able to see the description which I wrote, just like the standard Excel functions.
And finally, is there a way that I can provide a description for each argument which my function takes as input?
Here is the actual code, although I don't think it will be necessary to answer my questions.
Function removeNumbers(sInput As String, sChoice As Boolean) As String
Dim sSpecialChars As String
Dim i As Long
If (sChoice = True) Then 'if true is selected, will remove all number including 0
sSpecialChars = "0123456789" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
Next
End If
If (sChoice = False) Then 'if false is selected, will remove all numbers excluding zero
sSpecialChars = "123456789" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
Next
End If
removeNumbers = sInput
End Function
To make the function appear in the drop-down you must place it in a standard module rather than the worksheet code area.
Another poster has already covered the need for the code to be in a standard module. With regards the argument descriptions, you should look at the MacroOptions code in this answer - although it only works in Excel 2010 or later.
For Excel 2007 and earlier, the only solution I have seen is in an article by JK Pieterse. This involves using the ExecuteExcel4Macro and looks a bit complicated.

Excel VBA - Execute string as code

I am trying to execute the following string as code using Evaluate but get Error 2029. Anyone know why?
Help much appreciated.
Calculation = "Format(""21/08/2012"", ""MMM"")"
Value = Evaluate(Calculation)
Try instead
Calculation = "TEXT(""21/08/2012"", ""MMM"")"
EVALUATE converts formulas to results, and FORMAT is a VBA function. The formula equivalent is TEXT.
You can also skip the evaluate and use the FORMAT function on the date directly.
You can use most worksheet functions in VBA directly with Application.WorksheetFunction. - for example - try this out:
Sub DateExample()
Dim StringTest As String
StringTest = Application.WorksheetFunction.Text("12/08/2012", "MMM")
Cells(1, 1).Value = StringTest
End Sub
Good Luck

Resources