How do I enter special characters in Excel VBA? For example if I want "●" (large interpunct) in some strings, they all appear as "?". I tried to substitute all "●" with ● in VBA but it didn't work, simply displayed ● in the string.
Reference Website - Unicode and VBA’s ChrW() and AscW() functions
Here the code for bullet et strong bullet. Use hexadecimal in lieu of long.
Sub DisplayBullet()
Cells(1, 1).Value = "This is a bullet - " & ChrW(8226)
Cells(2, 1).Value = "This is a strong bullet - " & ChrW(&H25CF)
End Sub
Important: If you try to display in a MsgBox it's not working.
Related
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?
I'm working with Visual basic in Excel and I have a cell with the value "Gotovinski račun".
Another cell should be an English version of that value.
My code is:
If Range("A2").Value = "Gotovinski račun" Then Range("D2").Value = "Cash account"
Problem is that VBA can't recognize the small Lattin letter "č".
Any ideas?
Problem is that the VBA Editor only supports ANSI characters. You could try setting your "language for programs that do not support unicode" setting to Croation (or whichever language) (in administrative language settings).
Note that with that setting, the test string is properly displayed in the VBE:
If that is not an option, then you need to insert any characters with a code > 255 using the Chrw$ function. See the comment by #AlexK for an example.
A simple workaround consists in using a Like comparison with one (ore more) ? wildcards replacing the c plus caron character č:
With Sheet1.Range("A2")
If .Value Like "Gotovinski ra?un" Then
.Offset(0, 3).Value = "Cash account"
Else
.Offset(0, 3).Value = "'" & .Value & "' not found"
End If
End With
I have a cell that contains the text "Collar Lot #" and I would like to append a string onto the end of it that is not bold. An example would be "Collar Lot # 23456\34567\45678". My code attempt below makes the entire string bold, resulting in "Collar Lot # 23456\34567\45678"
With Workbooks(ThisWorkbook.name).Worksheets(1)
...
.Cells(7, 5).value = "Collar Lot # " & "23456\34567\45678"
End With
How would I make sure the "23456\34567\45678" was not bold?
You would need to specify the formatting based on the position of the characters by using something like:
.Cells(7, 5).Characters(Start:=1, Length:=13).Font.FontStyle = "Bold"
.Cells(7, 5).Characters(Start:=14).Font.FontStyle = "Regular"
The Record Macro button on the Developer Tab of the Ribbon is very useful for finding out something like this.
Visual Basic in Excel is pushing a part of the formula below on a second line. How do i keep it on one line or make it work on multiple lines?
ActiveCell.FormulaR1C1 = "=IF(ISNUMBER(SEARCH(""*DUPLICATE*"",RC[+3])),""Duplicate"",IF(ISNUMBER(SEARCH(""*ABBREV*AM or PM*"",RC[+3])),""Prohibited Abbreviation"",IF(ISNUMBER(SEARCH(""*ABBREV*>*<*"",RC[+3])),""Prohibited Abbreviation"",IF(ISNUMBER(SEARCH(""*ABBREV*Q*"",RC[+3])),""Prohibited Abbreviation"",IF(ISNUMBER(SEARCH(""*ABBREV*U*IU*"",RC[+3])),""Prohibited Abbreviation"",IF(ISNUMBER(SEARCH(""*Out of Stock*"",RC[+3])),""CMOP Out of Stock"",IF(ISNUMBER(SEARCH(""*SIG TOO LONG*"",RC[+3])),""Sig Too Long To Process"",IF(ISNUMBER(SEARCH(""*CMOP STOC*"",RC[+3])),""Quantity"",IF(ISNUMBER(SEARCH(""MISSPELLIN*"",RC[+3])),""Misspelling"",IF(ISNUMBER(SEARCH(""*MANUF*B*ORDER*"",RC[+3])),""Manufacturer'S Backorder"",IF(ISNUMBER(SEARCH(""*EXPIRED ADDRES*"",RC[+3])),""Expired Address"",IF(ISNUMBER(SEARCH(""*NOT STOCKED*LOW USAGE*"",RC[+3])),""Not Stock-Low usage"",IF(ISNUMBER(SEARCH(""*PRODUCT D*C*"",RC[+3])),""Product Discontinued"",IF(ISNUMBER(SEARCH(""*REFRIG*PO BOX*"",RC[+3])),""Refrig Item/PO Box Address"",IF(ISNUMBER(SEARCH(""*CORRECT*RESUBMIT*"",RC[+3])),""Correct Qty & Resubmit"","" "")))))))))))))))"""
You can split formula text across lines in the VBA editor using carefully-placed quotes, underscores and ampersands:
Option Explicit
Sub Test()
ActiveCell.FormulaR1C1 = "=IF(15>10, " & _ '<~ quote, ampersand and underscore
"12345, " & _
"67890)"
End Sub
That being said, ActiveCell can be tricky, and the formula you're writing out is... complex. Could you perhaps design a Select...Case statement in your design to result in a more readable experience for folks who might be maintaining your code?
This is really bugging me as it seems pretty illogical the way it's working.
I have a macro to format a cell as a currency using a bit of code to obtain the currency symbol.
Here is the code involved:
Dim sym As String
sym = reportConstants(ISOcode)
'Just use the ISO code if there isn't a symbol available
If sym = "" Then
sym = ISOcode
End If
With range(.Offset(0, 3), .Offset(3, 3))
.NumberFormat = sym & "#,##0;(" & sym & "#,##0)"
Debug.Print sym & "#,##0;(" & sym & "#,##0)"
End With
reportConstants is a dictionary object with currency symbols defined as strings. E.g. reportConstants("USD") = "$". This is defined earlier in the macro.
When the macro runs it gets the ISO code and should then format the cell with the corresponding currency symbol.
When I run it in one instance the ISO code is "USD" - so sym is defined as "$" - but it still formats the cell with a pound sign (£). When I debug.print the format cell string it shows $#,##0;($#,##0) so, as long as I got my syntax correct, it should use a dollar sign in the cell. But it uses a £ sign instead. (I am running a UK version of excel so it may be defaulting to £-sign, but why?)
Any help greatly appreciated.
I just recorded a macro to set the format to $xx.xx and it created this: [$$-409]#,##0.00. Looks like the -409 localises the currency to a particular country; it works without it - try changing yours to .NumberFormat = "[$" & sym & "]#,##0.00"
Btw guess I read your question somewhat after posting ;) Excel is well influenced by the regional settings of your computer for currency, language, dates... Using numberformat can force it to keep the sign you require. if it is a matter of rounding up you can try to: On Excel 2010, go to File - Options - Advanced and scroll down to "When calculating this workbook" and click on the "set precision as displayed" and OK out.
Try this: given your values are numerics/ integers/decimals....
Range("a2").Style = "Currency"
Or you can use format:
Format(value, "Currency")
Format(Range(a2).value, "Currency")
References:
http://www.mrexcel.com/forum/excel-questions/439331-displaying-currency-based-regional-settings.html
http://www.addictivetips.com/microsoft-office/excel-2010-currency-values/
(PS: I am on mobile, you may try these two links)