Cannot use "from" in .numberformat vba - excel

Hi I have 2 cells where user is entering values, placed next to each other like:
200 1200
Using .numberformat i want to change it to
From 200 To 1200
I can use any other word but "from" is giving me an Run-time error 1004
Code I wanted to use:
Range("B42:B44").NumberFormat = ("From" & "##0.0\°F")
Range("C42:C44").NumberFormat = ("To " & "##0.0\°F")
Any ideas how can I bypass this?

Test in a number format should be enclosed in quotes:
Range("B42:B44").NumberFormat = """From""##0.0""°F"""
or you can escape each character with \ as you did with the degree symbol, but that gets tedious!

This isn't a VBA problem. If you right click the cell and goto format/custom and type From ##0.0\°F you get an error. But regardless, you can fix it either manually or in VBA by escaping out the problem characters:
Range("B42:B44").NumberFormat = ("F\ro\m ##0.0°F")

Related

Why does using [#Name] in Excel VBA string cause error 1004?

When I try to execute this statement using Excel VBA, it gives an error 1004:
Cells(C, 2).Value = "=IFERROR(XLOOKUP([#Name],Master_List_1[Name],Master_List_1[Email]),""error"")"
If I remove the brackets around #Name, it works fine. The other brackets in the string don't cause trouble. Somehow VBA is parsing the text string and objecting to having [#Name] in there.
If I put the statement in a cell by hand (with single quotes), it works as it should. (I did forget the = sign; that's been corrected.

Setting Number Format to Accounting in VBA

With ActiveSheet
.Range("T26:T31").NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* " - "??_);_(#_)"
I am trying to set the format of a cell range to Accounting, I have looked at the number format (above) and tried setting it to that, but I get a Type Mismatch.
I also tried doing Debug.Print Application.ActiveCell.NumberFormatLocal to find out how excel reads it, copied that in and still with no luck.
Anyone got any ideas?
You need to double any quotation marks inside the format code, so:
.Range("T26:T31").NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* "" - ""??_);_(#_)"

Odd behaviour by automatically changing color of certain characters in a cell

I'm facing a odd behavior by applying different colours within one cell via VBA.
In my case there are hundrets of cells within one column, showing different work-packages.
My vba code exaclty does what it should do, by coloring identified strings (respecively work packages) via looping through the cells and identifiying each work package via RegExp.
Here there is one extract that is doing the coloring job:
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Global = True
.Pattern = suchmuster
If .test(objWks_myTable.Cells(row_myTable, 20).Value) Then
Set RegMC = .Execute(objWks_myTable.Cells(row_myTable, 20).Value)
For Each RegM In RegMC
objWks_myTable.Cells(row_myTable, 20).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = vbOrange
Next
End If
End With
The issue appears as soon as I double click the cell after my makro run.
Then without any recognizable pattern, some characters are shown in a different color (mostly not only one character but a connected bunch of). In the picutre, the first cell shows the colours after my vba run, the second cell shows how it will immediately look like, if i double click it.
If I leave the edit mode via Escape, the original vba set colors will stay, If I leave the edit mode via Return, the undefined changes happen.
There are no formats nor format conditions set within the cells.
I really need somebodys help here. Would be great to find a solution!
Many Thanks !
This picture should show the issue:
Picture of the issue
I've found the issue.
First I tried also Instr instead of using a RegExp but the issue didn't disappear.
So I was investigating in my code that writes the strings into the cells.
And within that code I did the following:
dummy = dummy & " # " & z_trim(ctrl.Caption) & vbCrLf
ActiveCell.Value = dummy
The issue is because of vbCrLf
If I write the strings into the cells the following way, the changes within my coloring run are fixed, there is no change by entering the cell in edit mode:
dummy = dummy & " # " & z_trim(ctrl.Caption) & Chr(10)
ActiveCell.Value = dummy
Picture of fixed issue
It works, so I'm fine. But still interessted, why vbCrLf is causing such confusing thing?

Excel error: expected end of statement, what does this mean?

I am trying to write this formula into a cell via my script:
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),"t","f")"
And it keeps coming up as an error, even though it works just fine in the actual sheet if I manually input it into the cell. What is it expecting me to do here?
You need to escape quotes. Try this:
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),""t"",""f"")"
The issue that you are running into is that " is interpreted as beginning or ending a VBA string. So VBA parses your expression as
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),"
with "garbage" at the end. This "garbage" is what it is complaining about. If you need to include a " within a VBA string, use "".

Handling errors

I am trying to perform some operations over some data, but they are not working and I need to find a way to ignore those cells which don´t meet all the requirements.
Basically, I have a column where some cells have text + numbers in their content and other have only text. I search inside all of them and split TEXT in one column and NUMBER in another one. Then, I run a macro to find the matching text to each one in other column.
But when I try to split TEXT from NUMBERS, I search for the first "(", cause my number format is (10.10.10), but if it is not found, the cell value appears: #VALUE! (ok, it is expected cause the character was not found). Here is the problem: if I run my macro over "#VALUE! it crashs and don´t finish its execution.
I have tried to use
On Error GoTo
in my macro code, but for some reason it doesn´t not handle the "Run time error: Type Mismatch".
For contadorOr = 2 To colO
For contadorDes = 2 To colA
On Error GoTo cont
If InStr(1, Cells(contadorDes, colunaDestino).Value, Cells(contadorOr, colunaOrigem).Value) Then
If InStr(1, Cells(contadorOr, colunaOrigem + 4).Value, Cells(contadorDes, colunaDestino + 1).Value) Then
Cells(contadorOr, colunaOrigem + 5).Value = "Mesma versão"
End If
Exit For
End If
Next contadorDes
cont: Next contadorOr
Any suggestions? I can think of ignoring this error (when it´s happen, my variable contadorOr is incremented and go to no next value) or any way to avoid #VALUE! returned by my functions, but haven´t had success doing that.
Thanks in advance.
Instead of using error handling options you could check if cell which you are going to check/process doesn't return error. This is quite simple as presented below:
If IsError(Cells(contadorDes, colunaDestino).Value) Then
'to do anything if there is error
'usually...do nothing
Else
'do what you want if there is no error
End if

Resources