加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VBA学习笔记之Text(四)——格式化字符串

(2012-10-15 08:30:00)
标签:

excel

format函数

数值显示格式

日期时间显示格式

自定义

分类: VBA
一、Format函数
 
         可以使用Format函数规定输出的字符串的格式,其语法为:

Format(expression [,format])

参数expression必须,为任何有效的表达式,其余参数均为可选。参数format表示所要采取的格式
二、字符串格式中的字符符号:
  • &:空或字符占位符,如果在输入的字符串的相应位置有字符,则显示该字符,否则不显示
  • @:空或字符占位符,如果在输入的字符串的相应位置有字符,则显示该字符,否则显示空格
  • <:将所有字符显示为小写格式
  • >:将所有字符显示为大写格式
  • !:强制占位符从左向右填满,满足默认为从右向左。
   演示代码:
Sub FormatDemo1()

Debug.Print Format(6666666, "(@@@)&&&-&&&&No Phone")
Debug.Print Format(6666666, "(&&&)&&&-&&&&No Phone")

Debug.Print Format(66666666, "(@@@)&&&-&&&&No Phone")
Debug.Print Format(66666666, "(&&&)&&&-&&&&No Phone")

Debug.Print Format(6666666666#, "(@@@)&&&-&&&&No Phone")
Debug.Print Format(6666666666#, "(&&&)&&&-&&&&No Phone")

Debug.Print Format("", "(&&&)&&&-&&&&No Phone")

Debug.Print Format("Hello", "<")
Debug.Print Format("Hello", ">")

End Sub
 
   运行结果:
( )666-6666
()666-6666
( 6)666-6666
(6)666-6666
(666)666-6666
(666)666-6666
No Phone
hello
HELLO

三、Format函数的简要使用规则:
  1. 允许用户预先定义或用户定义的格式来建立多种用于输出字符串、数字和日期/时间数据的方法
  2. 创建用户定义的数值格式最多可以有四个部分,每个部分代表一种不同类型的数值,且用分号分隔。第一部分在单独命名使用时可用于所有值,与其他多个部分一起使用时只用于正数;第二部分用于负数;第三部分用于零值;第四个部分用于Null值。
    • 参数中不必包括所有四个部分,但所有部分的数目决定了每个一个部分所定义的数值类型。只有一个部分,则应用于所有的数值;有两个部分,则第一部分应用于正数和零值,第二部分应用于负数;有三个部分,则第一部分用于正数,第二部分用于负数,第三部分用于零值;有四个部分时,同2.
    • 如果忽略了一个部分,则该部分使用与定义正数的部分一样的格式,如:"#.00;;#,##",表示负数值与正数值使用同一种格式显示。
    • 如果参数含有命名格式,则只能有一个部分。
  3. 字符串值的用户定义格式有两个部分,第一部分可应用于所有值,第二部分应用于Null值或零长字符串。

四、预定义的日期和时间格式

Sub FormatDemo2()

Debug.Print "General Date:" & Format("28/02/2012", "General Date")
Debug.Print "Long Date:" & Format("28/02/2012", "Long Date")
Debug.Print "Medium Date:" & Format("28/02/2012", "Medium Date")
Debug.Print "Short Date:" & Format("28/02/2012", "Short Date")
Debug.Print "Long Time:" & Format("17:30:03", "Long Time")
Debug.Print "Medium Time:" & Format("17:30:03", "Medium Time")
Debug.Print "Short Time:" & Format("17:30:03", "Short Time")
'预定义的数值格式:
Debug.Print "General Number:" & Format(1234.08933, "General Number")
Debug.Print "Currency:" & Format(123456.0789, "Currency")
Debug.Print "Fixed:" & Format(0.2, "Fixed")
Debug.Print "Standard:" & Format(123456.0789, "Standard")
Debug.Print "Percent:" & Format(0.7321, "Percent")
Debug.Print "Scientific:" & Format(123456.0789, "Scientific")
Debug.Print "Yes/No:" & Format(0, "Yes/No") '0为No,非0为Yes
Debug.Print "True/False:" & Format(0, "True/False") '0为False,非0为True
Debug.Print "On/Off:" & Format(0, "On/Off") '0为Off,非0为On

End Sub

   运行结果:

General Date:2012-2-28
Long Date:2012年2月28日
Medium Date:12-02-28
Short Date:2012-2-28
Long Time:17:30:03
Medium Time:下午 05:30
Short Time:17:30
General Number:1234.08933
Currency:¥123,456.08
Fixed:0.20
Standard:123,456.08
Percent:73.21%
Scientific:1.23E 05
Yes/No:No
True/False:False
On/Off:Off

五、创建用户自定义的日期和时间格式的字符

  • Sub FormatDemo3()

    '基于当前Windows系统的短日期和短时间国际设置格式的日期和时间
    Debug.Print "c: " & Format("28/02/2012 17:30:03", "c")

    '基于当前Windows系统的长日期国际设置格式的完整日期
    Debug.Print "dddddd: " & Format("28/02/2012", "dddddd")

    Debug.Print "/日期分隔符: " & Format("28/02/2012", "mm-dd-yyyy")
    Debug.Print "d: " & Format("28/02/2012", "d") '返回天
    Debug.Print "dd: " & Format("28/02/2012", "dd") '返回带有前导0的天
    Debug.Print "ddd: " & Format("28/02/2012", "ddd") '英文星期的简写
    Debug.Print "dddd: " & Format("28/02/2012", "dddd") '英文星期的全称
    Debug.Print "ddddd: " & Format("28/02/2012", "ddddd") '短日期格式
    Debug.Print "h: " & Format("05:08:06", "h") '小时
    Debug.Print "hh: " & Format("05:08:06", "hh") '前导0的小时
    Debug.Print "n: " & Format("05:08:06", "n") '分钟
    Debug.Print "nn: " & Format("05:08:06", "nn") '前导0分钟
    Debug.Print "s: " & Format("05:08:06", "s") '秒
    Debug.Print "ss: " & Format("05:08:06", "ss") '前导0秒
    '基于12小时制的时间,包含Windows区域设置中指定的时间分隔符和前导0.
    Debug.Print "ttttt: " & Format("05:08:06", "ttttt")
    Debug.Print "hh:mm:ss AM/PM: " & Format("17:08:06", "hh:mm:ss AM/PM")
    Debug.Print "hh:mm:ss am/pm: " & Format("17:08:06", "hh:mm:ss am/pm")
    Debug.Print "hh:mm:ss A/P: " & Format("17:08:06", "hh:mm:ss A/P")
    Debug.Print "hh:mm:ss a/p: " & Format("17:08:06", "hh:mm:ss a/p")

    Debug.Print "w: " & Format("02/02/2012", "w") '1~7分别表示星期天到星期六
    Debug.Print "ww: " & Format("02/02/2012", "ww") '一年中的第多少周(为1~64间的数字)

    Debug.Print "m: " & Format("02/02/2012", "m") '月(1~12范围内的数字)
    Debug.Print "mm: " & Format("02/02/2012", "mm") '前导0月
    Debug.Print "mmm: " & Format("02/02/2012", "mmm") '英文月的简写
    Debug.Print "mmmm: " & Format("02/02/2012", "mmmm") '英文月的全称

    Debug.Print "q: " & Format("02/02/2012", "q") '季度(1~4范围内数字)

    Debug.Print "y: " & Format("02/02/2012", "y") '一年中的第n天(1~365之间的数字)
    Debug.Print "yy: " & Format("02/02/2012", "yy") '年(00~99之间的数字)
    Debug.Print "yyyy: " & Format("02/02/2012", "yyyy") '年(1000~9999范围内的四位数字)

    End Sub

  运行结果:

  • c: 2012-2-28 17:30:03
    dddddd: 2012年2月28日
    /日期分隔符: 02-28-2012
    d: 28
    dd: 28
    ddd: Tue
    dddd: Tuesday
    ddddd: 2012-2-28
    h: 5
    hh: 05
    n: 8
    nn: 08
    s: 6
    ss: 06
    ttttt: 5:08:06
    hh:mm:ss AM/PM: 05:08:06 PM
    hh:mm:ss am/pm: 05:08:06 pm
    hh:mm:ss A/P: 05:08:06 P
    hh:mm:ss a/p: 05:08:06 p
    w: 5
    ww: 5
    m: 2
    mm: 02
    mmm: Feb
    mmmm: February
    q: 1
    y: 33
    yy: 12
    yyyy: 2012

六、用于创建用户自定义数字格式的字符

  1. 0:数字站位符。如果参数expression所代表的数值在相应的0位置上有一个数字,则显示这个数字,否则显示0.所指定的小数点后的位数,使数值舍入为给定的小数位数,但不影响小数点左边的数字位数。
  2. #:数字占位符。如果参数expression所代表的数值在相应的#位置上有一个数字,则显示这个数字,否则什么也不显示。
  3. .:小数点占位符。小数点占位符实际显示的字符由本机Windows系统国际设置格式决定
  4. %:百分数占位符。首先将参数expression所代表的数值乘以100,然后把它作为百分数显示。
  5. ,:千分位占位符。实际显示的字符由本机Windows系统国际设置格式决定。在格式定义中只需要给出一个千分位分隔符。如Format(1000000,”#,###“)返回:1,000,000
  6. E-E e-e :科学记数法格式。如果格式表达式在E-、E 或e-、e 的右边至少有一个数字占位符(0或#),数字就以科学记数法格式显示数字,参数Format中所用的字母E或e在该数字和它的指数之间显示。右边的数字占位符数目决定了要在指数中显示的位数。使用E-或e-可以在负指数前插入一个减号,使用E 或e 可以在正指数前插入一个正号。
  7. - $:显示一个直接量字符
  8. \:反斜杠后的字符以直接量字符显示。可以用反斜杠将某个特定格式的字符以直接量字符显示。

Sub FormatDemo4()

Debug.Print "0: " & Format(23.657, "0.0000")
Debug.Print "0: " & Format(2357, "00000")
Debug.Print "#: " & Format(23.567, "##.##")
Debug.Print "#: " & Format(12323.567, "#,###.##")
Debug.Print "%: " & Format(0.567, "##.00%")
Debug.Print "E-: " & Format(1.09837555, "#4##E-###")
Debug.Print "E : " & Format(109837.555, "#.##E-###")
Debug.Print "E: " & Format(109837.555, "#.##E-###")
Debug.Print "$: " & Format(234.25, "$#,###.##")
Debug.Print "\: " & Format(0.25, "##.##\%")
End Sub
 
   运行结果:
      0: 23.6570
0: 02357
#: 23.57
#: 12,323.57
%: 56.70%
E-: 10984E-4
E : 1.1E5
E: 1.1E5
$: $234.25
\: .25%

  注:格式代码可以叠加,如:

Sub ShowFormatVal()

Debug.Print Format(Now, """Today is ""dddd, mmmm d, yyyy" & _
""" the ""y""th day of the year. The time is now"" ttttt.")
End Sub
运行结果:Today is Saturday, October 6, 2012 the 280th day of the year. The time is now 19:19:54.

【应用】格式设置在表格式智能化上有很大帮助,尤其做日常报表的来说。制订好报表格式的宏,让代码为您的从繁复工作脱离,快哉快哉!

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有