vba - Excel VBA 格式化十六进制

标签 vba excel hex

我想将 3 位数字长的十六进制转换为 6 位数字。例如 12F 应为 00012F。我尝试了这段代码,但没有成功。

endadd = Format(endadd, "000000") 

最佳答案

正如 Scott Craner 指出的那样,简单的 Right("000000"& endadd,6) 可以很好地工作,但是 Right$("000000"& endadd,6) code> 稍微快一些。

此外,从性能角度来看,它实际上取决于 endadd 值的原始来源是字符串还是数字。

'CONCAT String Approach
'If the hex source is a string, then this is the most efficient approach
formattedHex = Right$("000000" & "12F", 2)

'CONCAT Numeric Approach
'But if the hex source is a numeric, then this hex conversion AND concatenation is required, but it is SLOW
formattedHex = Right$("000000" & Hex$(&H12F), 2)

'ADDITION/OR Numeric Approach
'When the hex source is numeric it is more efficient to use a bit trick to add AND convert
formattedHex = Right$(Hex$(&H1000000 + &H12F), 2)
formattedHex = Right$(Hex$(&H1000000 Or &H12F), 2)

10m 操作循环的结果:

Approach                  | Using Right | Using Right$ |
==========================+=============================
CONCAT String Approach    | 1.59s       | 1.40s
CONCAT Numeric Approach   | 2.63s       | 2.33s
ADDITION Numeric Approach | 1.74s       | 1.60s
======================================================

关于vba - Excel VBA 格式化十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323037/

相关文章:

excel - 如何按特定顺序将 Excel 注释打印到多个工作表?

vba - 使用随机数进行计算

vba - 将文本框复制到剪贴板

Python win32 Excel 将图表粘贴为位图 (PasteSpecial)?

c++ - 以十六进制读取文件会给出错误的输出(跳过一些十六进制值)

string - 如何在MS Access中使InStr区分大小写

excel - 调试发现错误 91 与 intersect(target, [range variable]).value

c# - "Column out of Range"填充 Excel 工作表时出错

javascript - 将字符串编码为 HEX

powershell - 从文件中格式化 powershell Get-Content 字节代码