我希望当我重复按快捷键时,单元格中的数字会自动在以下格式之间切换:
2020年
2020A
2020年
2020年上半年
2,020%
2,020x
2,020bps
Sub Number_Format_Cycle()
Dim x, myFormats
myFormats = Array("###0;(###0);-", "###0A;(###0)A;-", "###0E;(###0E);-", "###0A/E;(###0A/E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0bps;(#,##0)bps;0bps")
x = Application.Match(ActiveCell.NumberFormat, myFormats, False)
If IsError(x) Then x = 0
Selection.NumberFormat = myFormats((x Mod (UBound(myFormats) + 1)))
End Sub
最佳答案
您必须使用反斜杠 (\
) 来添加字 rune 字,使它们完全按照键入的内容显示。
将 myFormats
分配更改为以下代码行对我有用:
myFormats = Array("###0;(###0);-", "###0\A;(###0)\A;-", "###0\E;(###0\E);-", "###0\A\/\E;(###0\A\/\E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0\bp\s;(#,##0)\bp\s;0\bp\s")
在针对数组元素进行测试设置后,重新检查您打算添加的每个数字格式。
警告:我必须承认我无法解释最后一个 \bp\s
格式,但它确实有效。
关于excel - 如何循环选择数字格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72689336/