Excel VBA(Visual Basic)编程入门

Visual Basic for Applications(VBA),是依附在应用程序(例如Excel)中的VB语言。只要你安装了Office Excel就自动默认安装了VBA,同样Word和PowerPoint也能调用VBA对软件进行二次开发而让一些特别复杂的操作“脚本化”。VBA是Excel的底层根本。作为程序员来讲,只有明白使用最底层的东西,使用起来才能随心所欲。

一、VBA打开方式

方式一:快捷键ALT+F11
方式二:

sheet 右击–>查看代码 可以进入编程界面

方式三:文件 - 选项 - 自定义功能区 - 勾选开发工具 -菜单栏点击 “开发工具”-Visual Basic

进入下面的代码编辑页面

二、 写一个 Hello World

大部分程序入门都会写一个代码输出“Hello World”,我们写第一个程序在选定的单元格输出自己的昵称。

1、新建模块

模块方便我们导出代码用于其他的Excel,所以养成良好的编程习惯插入模块

2、 在指定区域编写代码
Sub class() Dim name name = 'Hello World' MsgBox name End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

三、注释

注释有2种形式,一种是Rem;一种是 (单引号),注释不影响代码执行

Sub class()
Rem 这是注释
    '这是第二种类注释
'定义一个变量
    Dim name
    name = '清平乐'
    MsgBox name
End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

四、调用'立即窗口' 和 “本地窗口”

1.在工具栏中选择视图—>立即窗口,和本地窗口。
可以理解为打印台

Sub class() Rem variant是任意类型 Dim name As Variant name = 'Hello World' Rem debug Debug.Print name name = '清平乐' Debug.Print name Rem 定义常量 Const num As Integer = 123 Debug.Print num End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
2.debug显示
Sub class()
    Dim name
    name = 'Hello World'
    Debug.Print name
    name = '清平乐'
    MsgBox name
    Debug.Print name
End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

五、数据类型

variant 代表任意类型

single double decimal 代表 小数、

rem 代表注释关键字

Const 常量关键字


六、分支语句

IF分支

Sub score() Dim 定义一个变量初始值 Number = 90 If Number >= 90 Then Debug.Print '优秀' ElseIf Number >= 80 Then Debug.Print '良好' Else Debug.Print '一般' End If End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

<>

Sub class3()
Dim number As String
number = '匹配'
If number > '匹配' Then
   Debug.Print '优秀'
ElseIf number <> '匹配' Then
   Debug.Print '良好'
End If
End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

switch case

Sub class4() Dim number As String number = '匹配' Select Case number Case '匹配' Debug.Print '匹配' Case '不匹配' Debug.Print '不匹配' End Select End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

七、循环语句

1.For 循环
Sub class()
Rem 演示for 循环
Dim count As Integer
    For count = 1 To 10
        Debug.Print count
    Next
        Debug.Print 'count 循环结束之后的值是 ' & count
End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
2.do while
Sub class() Rem do while 演示 Dim count As Integer count = 20 Do While count > 10 Debug.Print count count = count - 1 Debug.Print count Loop Rem do .. loop 条件 不演示了 Do Loop While count > 10 End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
3.退出循环

(1)退出for 循环—— exit for

Sub class1()
    Dim count As Integer
    For count = 1 To 10
        If count = 5 Then
            Debug.Print 'count 退出循环的值是: ' & count
            Exit For
        End If
        Debug.Print count
    Next
End Sub
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


(2)退出do while 循环——exit do

Sub class1() Dim count As Integer Do While True count = count + 1 If count > 5 Then Debug.Print '此时退出循环的值是: ' & count Exit Do End If Loop End Sub

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

八、操作单元格

两种方式,一种是range,一种是cells
range

cells

(0)

相关推荐