Using "Special Characters" in VBA

rcarmichael

New Member
Joined
Aug 10, 2012
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Good afternoon,
I have some VBA triggered by a button in a workbook that will automatically hide/show a series of columns (A:CC) if the corresponding cell in ROW(8) contains "NZ".
VBA Code:
Private Sub NZToggle_Click()

Dim SrchRng As Range, cel As Range

If NZToggle.Caption = "Hide NZ" Then
        'Hide "New Zealand"
     
Set SrchRng = Range("A8:CC8")

For Each cel In SrchRng
    If InStr(1, cel.Value, "NZ") > 0 Then
        cel.EntireColumn.Hidden = True
    End If
Next cel

  NZToggle.Caption = "Show NZ"
Else
        'Show "New Zealand"
     
Set SrchRng = Range("A8:CC8")

For Each cel In SrchRng
    If InStr(1, cel.Value, "NZ") > 0 Then
        cel.EntireColumn.Hidden = False
    End If
Next cel

  NZToggle.Caption = "Hide NZ"
End If
End Sub

This code works well, but instead of searching for the text "NZ", I want to search for the special character/s "🇳".
I have tried replacing "NZ" with "🇳" by pasting in the special character/s but it results in "????".
I then tried replacing "NZ" with UNICHAR(127475) which results in a "Compile error: Sub or Function not defined" on the Unichar step.
I then tried replacing "NZ" with Chrw(127475) which results in a "Run-time error '5': Invalid procedure call or argument" on the If InStr(1, cel.Value, ChrW(127475)) > 0 Then step.

Any idea what I'm doing wrong, or if what I'm trying to do is even possible?
Sincere regards,
Ryan
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Someone else may be able to give you a better answer but either of these worked for me:
(use your own cell reference)
VBA Code:
Dim unicodeValue As Variant
unicodeValue = Evaluate("=Unichar(127475)")
Debug.Print InStr(1, ActiveCell, unicodeValue)

Using the function from here:
Section heading Excel / VBA

VBA Code:
Debug.Print InStr(1, ActiveCell,  UnicodeFromInt(127475))

The function to insert being:
VBA Code:
'https://sqlquantumleap.com/2019/06/26/unicode-escape-sequences-across-various-languages-and-platforms-including-supplementary-characters/
Function UnicodeFromInt(val As Long)
    If val < 0 Or val > 1114111 Then
        UnicodeFromInt = "ERROR: value must be between 0 and 1114111!!"
        GoTo GetOut
    End If
 
    If val >= 55296 And val <= 57343 Then
        UnicodeFromInt = "ERROR: surrogate code points are not displayable!!"
        GoTo GetOut
    End If
 
 
    If val < 65536 Then
        UnicodeFromInt = ChrW(val)
    Else
        UnicodeFromInt = ChrW(55232 + Int(val / 1024)) & ChrW(56320 + Int(val Mod 1024))
    End If
 
GetOut:
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,224,812
Messages
6,181,105
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top