Padlock symbol through vba

2019-08-21 07:38发布

Is it possible to print a padlock symbol (open & closed) in a cell with Chr(), ChrW() or something similar through VBA?

标签: excel vba
2条回答
Emotional °昔
2楼-- · 2019-08-21 08:30

It seems that Excel stores special chars as UTF-16. As long as you have a code that fits within 16bit, it's easy: Just convert the code using function ChrW. For example, the code for an alarm clock is U+23F0, so you can write ActiveCell.Value = ChrW(&H23F0) and you get your alarm clock (the prefix &H defines a hexadecimal number).

However, when you have characters that doesn't fit into these 16bit, you have to figure out the UTF-16 codes and concatenate them. The locks I found are U+1F512 and U+1F513, and their UTF-16 representatives are 0xD83D 0xDD12 resp. 0xD83D 0xDD13. So you write something like

ActiveCell.Value = ChrW(&HD83D) & ChrW(&HDD12)

Of course, you need to set a font to the cell that supports these characters.

I found http://www.fileformat.info/info/unicode/char/search.htm helpfull to find matching characters. If you have already a character and you can paste it into a cell, you can use the code I've written in this answer: https://stackoverflow.com/a/55418901/7599798

查看更多
干净又极端
3楼-- · 2019-08-21 08:33

You need to add a unicode symbol. The following code will add a lock or unlock symbol in cell 1,1.

Sub SetUnlock()
    Sheet1.Cells.Font.Name = "Segoe UI Symbol"
    Sheet1.Cells(1, 1).Value = ChrW(&HE1F7)
End Sub

Sub SetLock()
    Sheet1.Cells.Font.Name = "Segoe UI Symbol"
    Sheet1.Cells(1, 1).Value = ChrW(&HE1F6)
End Sub

You can use the "Add Symbol" button in the "Insert" ribbon to look for other symbols in different fonts.

查看更多
登录 后发表回答