假如想通过VBA对EXCEL当前表A列单元格或区域的数字类型进行批量格式化处理,方法如下:
r = activesheet.range(“A1”).currentregion.rows.count
for i = 2 to r
ActiveSheet.range(“A1”).NumberFormatLocal = “@”:
next
NumberFormatLocal 常用的格式代码:
Selection.NumberFormatLocal =“G/通用格式” '常规
Selection.NumberFormatLocal = “0.00_ " '数值
Selection.NumberFormatLocal = “¥#,##0.00;¥-#,##0.00” '货币
Selection.NumberFormatLocal = “_ ¥* #,##0.00_ ;_ ¥* -#,##0.00_ ;_ ¥* " & “””” & “-” & “”“” & "??_ ;_ @_ " '会计专用
Selection.NumberFormatLocal = “yyyy/m/d;@” '日期
Selection.NumberFormatLocal = “[KaTeX parse error: Expected 'EOF', got '#' at position 101: …FormatLocal = "#̲ ?/?" '分数 Selec…-804]G/通用格式” '中文大写 '特殊
Selection.NumberFormatLocal = “[DBNum2][
R
M
B
]
G
/
通
用
格
式
;
[
红
色
]
[
D
B
N
u
m
2
]
[
RMB]G/通用格式;[红色][DBNum2][
RMB]G/通用格式;[红色][DBNum2][RMB]G/通用格式” ''特殊人民币大写
Selection.NumberFormatLocal = “yyyy/mm/dd” '日期 '自定义
如果想将文本型数值批量赋给单位格,并保持文本型类型,语句示例如下:
如,将数组中的文本型数值保存在表格中单元格A列
dim arr() as string
dim key as string
dim i as integer
key = “1001,1001.01,1002,1002.01,1220,1221”
arr=split(key,“,”)
for i = 1 to ubound(arr)
activesheet.range(“A” & i).NumberFormatLocal = “@”: rem 赋值前对单元格数值类型进行设置
activesheet.range(“A” & i).value = arr(i)
next