vba 对Excel 表格的操作

vba 基础语法:

变量的定义:

变量:Dim   xxdim xx,mm    dim  xxx  as string

定义常量 :const x  

数据类型

dim x

x = array("xx","yy")

 循环

for i = 1 to 

嘻嘻嘻

next

for each j  in xxx

xxxx

next

do while xxx

xxx

exit do 跳出循环

loop

if 判断

if  xxx  then

end if 

if xxx  then  else  end if 

函数调用 

sub test(x)

xxxx

end sub 

test  x

function demo()

xx

end function

demo()

异常处理:

on error goto error_miaoshu

xxxxx

error_miaoshu     异常处理直接跳到指定的地方

on error resume next   遇到报错,直接走下一行

excle

根据表名获取对象:  set  sheet_obj = sheets("表名")   sheet_obj.cells(1,1) = 8

当列的有效行数: max = sheet_obj.cells(rows.count,"G").end(xlup).row  

当行有效列数; ma小 = sheet_obj.cells(5,columns.count).end(xlup).column

整个表有效行数:sheet_obj.usedrange.rows.count

整个表有效列数:sheet_obj.usedrange.columns.count

一列 数据对象:dim arr as variant

arr = range("A5:A100")