VBA基础
1.VBA减少缩进量:shift+tab
Sub Mycode() Dim i As Integer Dim isbool As Boolean Dim a As String For i = 2 To 10 a = Cells(i, 1).Value MsgBox a isbool = Cells(i, 1) = "" If isbool Then Cells(i, 1) = Cells(i - 1, 1) End If Next i End Sub
2.简单编程,以上代码实现excel表第一列2-10行,从2至10如有空,则填充为上一格的值;
函数cells(行号,列号或列字母(“A”)),返回指定单元格的值,Cells(i, 1).Value与Cells(i, 1)效果相同;
3.VBA逻辑变量:Boolean
Sub ColorShow() Dim c As Long Dim col As String Dim l As Integer Dim i As Integer MsgBox i ' 返回指定单元格颜色数据 c = Range("A1").Interior.Color MsgBox c Cells(1, "A") = c For i = 166 To 168 MsgBox i col = "A" & Str(i) l = Len(i) 'replace函数,替换字符串中指定内容;
'此处有疑问i长度系统反馈为2,与"A"组合后,长度为三位,其中有空格,显示为 “A 2”,估用replace函数替换中间空格;有空格原因不知,希望有大神能告诉一下原因 col = Replace(col, " ", "") l = Len(col) MsgBox l MsgBox col Range(col).Interior.Color = 13408767 Next i End Sub
4.VBA数据类型:
二进制:1个字节8bit,十进制:1个字节2^8
string:字符串,4个字节
byte:字节型,1个字节
Boolean:布尔型数据逻辑值,2个字节
应当注意的是,当其它数据类型转换为布尔值时,0会转成False,其它值则变成True。当把布尔值转换成其他数据类型时,False会转换为0,True则是-1。
integer:整型,2个字节,-32768 ~ 32767
long:长整型,4个字节,-2147483648 ~ 2147483647
single:单精度浮点型 ,4个字节
在表示负数时: -3.402823E38 ~ -1.401298E-45
在表示正数时: 1.401298E-45 ~ 3.402823E38
double:双精度浮点型 ,8个字节
在表示负数时: -1.79769313486231E308 ~ -4.94065645841247E-324
在表示正数时: 4.94065645841247E-324 ~ 1.79769313486231E308
Date:日期型数据不仅可以表示日期,还可以表示时间。可以表示的日期范围是:100年1月1日 ~ 9999年12月31日;可以表示的时间范围是:0:00:00 ~ 23.59.59。占用8个字节。
Variant:变体型数据是一种特殊的数据类型,几乎可以用于保存所有其它数据类型的数据。可以简单地理解为:当不知道变量所要表示的数据是什么类型时,就把它定义为Variant。
Object:对象型是VBA中另一种特殊的数据类型。有点类似于其它高级编程语言中的“对象”,它们都有自己的属性与方法.
其他隐式说明
% integer 整型 & long 长整型 $ string 字符串 ! single 单精度浮点 # double 双精度浮点 @ currency 货币型

浙公网安备 33010602011771号