QINMS.com || 首页 ||

EXCEL-VBA代码(二)

7.处理字符串:

格式:left(string,length)

说明:

Left:返回左侧指定数量的字符

  left(hello,1)=h

right:返回右侧指定数量的字符

  right (hello,1)=o

mid:返回字符串指定位置开始,指定长度的字符

  mid(hello,2,2)=el

instr:返回一字符串在另一字符串中最先出现到的位置

  instr(3,“hello Excel”,“e”)=10

  (若后加 vbtextcompare,则忽略大小写)

ltrim:返回去掉左侧空白的字符串

rtrim:返回去掉右侧空白的字符串

8.生成重复空格:

Space(n)

9.生成重复的由单个字符组成的单个字符串:

String(number,character)

character为一串字符时,只对第一个字符有效,如:

String(5,"abc")=aaaaa

10.计算字符串长度:

Len(string|varname)

常用len(string)=0 来判断某个字符或者参数是不是空的。

11.将小写的金额转化为大写的金额:

Sub 大写金额()

Dim t as currency,str1 as string

Dim I as ineger,strj as string,strf as string

Dim rng1 as range

With activesheet

   Set rng1=range("iv1").end(xltoright) 获取最右则列

   t=activecell.value

   With rng1

     .value=t

     .nomberformatlocal="[DBNum][$-804]G/通用格式"

     .columns.autofit

     Str1=.text

   End with

   i=instr(str1,".")

   If i>0 then

   Strj=mid(str1,i+1,1)

   Strf=mid(str1,i+2,1)

   If strf="" then

   str1=left(str1,i-1)&"元" &strj & "角整"

   Else

   str1=left(str1,i-1)&"元" &strj & "角"& strf & "分"

   End if

   Else

   Str1=str1 & "元整"

   End if

   Activecell="人民币" & str1

End with

End sub

上一页1】[2]【3下一页

© 2013-2023 版权所有: 村美小站 || 验证码 || 版权声明 || 免责声明 || About Us || 电子邮箱:
微 信微 博