1.VBA语言基础——《Excel VBA教程完全版》

1.1 运算符

  逻辑/位运算符:not  and  or 

  关系运算符:like  is

1.2 数据类型

  字符串    string  $

  字节      byte

  布尔      boolean

  整数      integer  %

  长整数    long  &

  单精度    single  !

  双精度    double  #

  日期      date 

  货币      currency  @

  小数点    decimal

  变体      variant

  对象      object

1.2    变量与常量

VBA允许使用未定义的变量,默认是变体变量。

在模块通用说明部分,加入 option explicit 语句可以强迫用户进行变量定义。

变量定义语句:

  dim xyz as integer  ‘局部变量

  private  xyz as byte  ‘私有变量

  public xyz as single   ‘公有变量

  global xyz as data   ‘全局变量

  static xyz as double   ‘静态变量

常量定义语句:程序中不能改变值

  const Pi=3.1415926 as single

1.3    数组

  dim array1() as double   ‘定义动态数组

  redim array1(5)    ‘重新改变数组大小为5

  array1(3)=250    ‘修改数组元素

  redim preserve array1(5,10)  ‘改变数组大小为二维数组,同时保留原来内容

1.4    注释与赋值

注释语句:  单引号 ’ 注释内容

赋值语句:

  X=123  ‘变量赋值

  Set myobject=objece  ‘对象赋值

  Myobject:=object   ‘对象赋值

1.5    书写规范

VBA不区分大小写

一行可写多条语句,用冒号:隔开

一条语句可写多行,用空格加下划线 _表示下行为续行

1.6    判断语句

  If a>b and c>d then a=b+2 else c=c-5 

  If number<10 then

      Digits=1

  Elseif number<100 then

      Digits=2

  Else

      Digits=3

  End if

  Select case pid

      Case “A101”

      Price=200

      Case “A102”

      Price=300

      …

      Case else

      Price=900

  End case

1.7    循环语句

  For words=10 to 1 step -1

      For chars=0 to 9

          Mystring=mystring&chars

      Next chars

      Mystring=mystring&” ”

  Next words

  For each range2 in range1

      With range2.interior

          .colorindex=6

          .pattern=xlsolid

      End with

  next

1.8    错误语句处理

  On error goto line   ‘错误发生时,会立即转移到line行去

  On error goto 0   ‘错误发生时,立即停止过程中任何错误处理过程

1.9    过程和函数

  Sub password (byval x as integer, byref y as integer)  ‘byval按值传递  byref按地址传递

      If y=100 then y=x+y else y=x-y

      X=x+100

  End sub

  Sub call_password()

       Dim x1 as integer

       Dim y1 as integer

       X1=12

       Y1=100

       Call password(x1,y1)  ‘调用password过程

       Debug.print x1,y1  ‘x1按值传递未改变原值为12,y1按地址传递改变原值为112

  End sub

  Function password(byval x as integer, byref y as integer) as Boolean  ‘函数返回值为布尔

      If y=100 then y=x+y else y=x-y

      X=x+100

      If y=150 then password =true else password=false

  End function

  Sub call_password()

      Dim x1=12

      Dim y1 =100

      If password(x1,y1) then  ‘调用函数,得到F/T作为if参数使用

        Debug.print x1

      End if

  End sub

1.10内部函数

  Isnumeric(x)  ‘判断是否为数字

  Isdate(x)

  Isempty(x)  ‘是否为空值

  Isnull(expression)  ‘表达式是否不包含有效数据

  Iserror(expression)  ‘表达式是否为一个错误值

  Sin(x)  tan(x)  ‘三角函数

  Log(x)  exp(x)  ‘常规函数

  Abs(x)  sqr(x)  rnd(x)  ‘绝对值  平方根  随机数(0-1之间单精度数据)

  Int(x)  fix(x)  ‘返回参数的整数部分

  Trim(string)  ‘去掉字符左右两端空白

  Lreim(string)  ‘去掉字符左端空白

  Rtrim(string)  ‘去掉字符右端空白

  Len(string)  ‘计算字符长度

  Left(string,x)  ‘取字符左端x个字符串

  Right(string,x)  ‘取字符右端x个字符串

  Mid(string,start,x)  ‘取字符中间从start位开始的x个字符串

  Cbool(expression)  ‘转化为布尔

  Cbyte(expression)  ‘转化为字节

  Ccur(expression)  ‘转化为货币

  Cdate(expression)  ‘转化为日期

  …             ‘同理对待single double integer long string variant decimal

  Val(string)   ‘转化为数据型

  Str(number)  ‘转化为字符

  Now()  ‘现在日期

  Date()

  Time()  ‘当前时间

  Timeserial(hour,minute,second)  ‘包含具体时、分、秒的时间

  Datediff(interval,date1,date2)  ‘两个指定日期间的时间间隔数目

  Minute(time)  ‘提取time中的某分钟

  Hour(time)  ‘提取time中的某小时

  Day(date)  ‘提取date中的某日

  Month(date)  ‘提取date中的某月

  Year(date)  ‘提取date中的某年

  Weekday(date)  ‘换算date中的星期

猜你喜欢

转载自www.cnblogs.com/data-science-chinchilla/p/8974312.html
vba