Excel VBA - Execute string as code - excel

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

Related

Formula error due to different Languages in VBA 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

VBA #VALUE!" error while using a Function

Still one hair remaining... not for long
This :
Function Around(Compare As String)
Around = (Range(Compare).Value = Range(Compare).Offset(-1, 0).Value) Or (Range(Compare).Value = Range(Compare).Offset(1, 0).Value)
End Function
generates a #VALUE! in the cell that calls it
I cannot figure out why
Any clues ?
I think #Value error while accessing user defined function in VBA does not apply here.
I'm guessing you're typing the formula in like =Around(E8), when you need to type it in as =Around("E8") since Compare is a String:
If you want to type it without quotation marks, then you need to declare Compare as a Range and change some syntax:
Function Around(Compare As Range)
Around = (Compare.Value = Compare.Offset(-1, 0).Value) Or (Compare.Value = Compare.Offset(1, 0).Value)
End Function

chanaging an IF and Weekday formula into a vba

I need help to change the following function into VBA code. This will be part of a larger code.
IF((WEEKDAY($B12)=7),$I12,"")
There are probably more than 5 ways to do what you want, depending on what exactly do you need. One of these ways is to build a simple custom formula like this:
Public Function changingIfAndWeekday() As Variant
Application.Volatile
If Weekday(Range("B12")) = 7 Then
changingIfAndWeekday = Range("I12")
Else
changingIfAndWeekday = ""
End If
End Function
You could also do it like so (if you want the result on cell C12):
Sheet1.range("C12").value = "=IF(Weekday(Sheet1.range("B12").value = 7),Sheet1.range("I12").value,"")
You could also do it like so (if you want the result on a variable):
Variable = "=IF(Weekday(Sheet1.range("B12").value = 7),Sheet1.range("I12").value,"")

Extract URL from =Hyperlink()

I'm trying to extract the formula that is evaluated from a link generated with a formula:
=HYPERLINK("https://www.google.com/search?q="&A1&B1,"Link")
(A1 has vba and B1 has help)
I've seen many, many threads and even some SO threads with suggestions, but they only get me to the ...q= without considering I have more text to come.
The best luck I've had so far is from this thread, and I've tweaked it to search until B1 like this:
...
S = Left(S, InStr(S, "B17") + 2)
...
But it returns https://www.google.com/search?q="&A1&B1.
How can I get it to first evaluate what's in those cells, before returning the URL?
I was overthinking this I think. Thanks to #MacroMan for getting my head straight. I put together the following, rather clunky, macro.
Function hyperlinkText(rg As Range) As String
' Inspired by https://stackoverflow.com/questions/32230657/extract-url-from-excel-hyperlink-formula/32233083#32233083
Dim sFormula As String
Dim Test As String
sFormula = rg.Formula
Test = Mid(sFormula, WorksheetFunction.Search("""", sFormula), WorksheetFunction.Search(",", sFormula) - WorksheetFunction.Search("""", sFormula))
hyperlinkText = Evaluate("=" & Test)
End Function
This can take a URL that looks like:
=HYPERLINK("https://www.google.com/search?q="&A17&B17,"Link") and return the evaluated URL:

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.

Resources