27

学习VBA

 4 years ago
source link: https://www.tuicool.com/articles/BBbMFvR
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

学习VBA

VBA 就是 (Visual basic for Application) 用的比较多的是在Excel中处理数据,可以方便快捷地使用编程方式来对数据进行操作。

VBA 数据类型

Integer

Public Sub fun()
        Dim num As Integer, total As Integer ' 九九乘法表
        
        For i = 1 To 9
            For j = 1 To i
                Debug.Print i & " * " & j & " = " & (i * j)
            Next j
        Next i
    End Sub

Boolean

Dim isExists As Boolean
    isExists = False
    isExists = True

Single

Single主要是表示小数,而Integer这个表示整数,Double比Single表示更长,在一些高级语言中,Single也是表示float类型。但是在VBA中没有float类型。

Dim agePrecent As Single    
    agePrecent = 12.6

Double

Dim agePrecent As Double
    agePrecent = 12.6

String

String也是一个基本类型

Dim userName As String
    userName = "Xiao ming"
    If userName <> empty or userName <> "" Then
        'TODO
    ElseIf userName <> "Xiao ming" Then
        'TODO
    Else
        MsgBox "你是" & userName
    End If

我们可以发现,基本类型都是用 变量名 = 值;但是如果是对象,我们就需要用到 Set关键字。Set 变量名 = 值。我们在连接字符串或者说是字符串拼接的时候,我们用 & 进行拼接。

Object

对象类型,对象类型的赋值,需要使用set进行。

Dim dic As Object
    Set dic = CreateObject("scripting.dictionary") '创建一个字典

    dic.Exists(key) ' 键是否存在
    dic.Item(key) '通过键读取某个值
    dic.Keys() '获取所有的键
    dic.Items() '获取所有的值
    dic.Add key,value ' 添加键值

  在Office中我们使用开发工具进行编码,添加模块的时候,有一个模块和类模块的区分。所谓模块,我们可以认为是一个公共接口,只要里面定义的方法是Public的,那么其他任何地方都可以调用。如果我们定义的是类模块,那么他就应该具有类的特性,需要创建实例才能用。

Array

VBA中没有一个类型来定义数组,但是数组使用也是比较广泛。值得注意的是,和其他编程语言不一样,在VBA中数组起始的索引号是从1开始的。

Dim arr (1 To 10) As Integer ' 一维数组
    Dim index As Integer
    For index = 1 to 10
        arr(index) = index
    Next index

    Dim arr(1 To 2, 1 To 5) As String ' 定义二维数组

    ' 定义一个函数,返回类型为数字类型,统计数组长度。
    Public Function Len(ByVal arr) As Integer
        Len = UBound(arr) - LBound(arr) + 1
    End Function

Variant

可变类型,所谓可变类型,就是我们可以定义其为任意类型,类似JavaScript中的Var

函数

无返回类型的函数

我们在定义无返回类型的函数的时候,通常用Sub关键字,但是我们可以用使用Function这个关键字的。Function是用于有返回类型的函数中。函数的定义:访问级别 函数类别 函数名([params] parameters)

Public Sub Test1()
        Debug.Print "Test"
    End Sub
    
    '上面的函数也可以修改为  
    Public Function Test1()
        Debug.Print "Test"
    End Function

    Private Sub Cal(num As Integer, num2 As Integer)
        Debug.Print (num * num2)
    End Sub

    ' 调用方法
    Call Cal(12, 13)
    ' 或者
    Cal 12, 13
    ' 调用无参的函数就不能省略Call关键字
    Call Test1()

有返回类型的函数

定义有返回值的函数,我们必须使用Function关键字,我们将结果赋值给函数名,如果是返回的对象,不要忘记使用Set关键字。

Public Function Add(a1 As Integer, a2 As Integer) As Integer
        Add = (a1 + a2)
    End Function

    ' 创建一个字典  
    Public Function CreateDic() As Object
        Set CreateDic = CreateObject("scripting.dictionary")
    End Function

    ' 调用
    Dim resVal As Integer
    resVal = Add 13,13
    Debug.Print resVal

    Dim dic As Object
    Set dic = CreateDic()

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK