VBA基本语法整理 您所在的位置:网站首页 VBA程序代码注释 VBA基本语法整理

VBA基本语法整理

#VBA基本语法整理| 来源: 网络整理| 查看: 265

VBA基本语法整理

作者:  Zjmainstay

最近为了一个调货程序,边学边用,终于是写出来了一个Excel VBA程序,其中学会了不少使用方法,借此整理出来,希望能够给后来的新人有所帮助。

1. VBA 局部变量和全局变量 1. 局部变量 在Sub或者Function结构中定义的变量 通常使用Dim关键词来声明,但是可以不声明使用(不报错) 2. 全局变量 在Sub或者Function外部(尽量在整个代码顶部),使用Public关键词定义 Public name '普通全局变量 Public styleDic As Object '字典全局变量 2. VBA 变量赋值 普通变量,直接用: name = "Zjmainstay" 赋值即可。 定义一个字典变量: Dim styleDic As Object Set styleDic = CreateObject("Scripting.Dictionary") 3. VBA 选中一个Sheet ThisWorkbook.Sheets(2).Select ’2表示第2个Sheet,下标从1开始 4. VBA 获取单元格内容 val = ThisWorkbook.Sheets(1).Cells(rowNum, colNum) 或: val = ThisWorkbook.Sheets(7).Range("A" & rowNum) colNum是数值,A与colNum对应英文字母,A对应1,B对应2,以此类推 5. VBA 获取单元格行号和列号 colNum = ThisWorkbook.Sheets(1).Range("A1").Column rowNum = ThisWorkbook.Sheets(1).Range("A1").Row 注:使用Cells获取也可以 6. VBA 单元格赋值 ThisWorkbook.Sheets(1).Cells(rowNum, colNum) = val 或 ThisWorkbook.Sheets(7).Range("A" & rowNum) = val 7. VBA Range获取单元区间   For Each cellVal In ThisWorkbook.Sheets(1).Range(startColName & rowNum & ":" & endColName & rowNum)'cellVal即单元格的内容'cellVal.Column 列号'cellVal.Row 行号Next 8. VBA 使用Find搜索单元格内容

在使用Find的时候经常会遇到两个问题:  1. VBA Find搜索失败,抛出异常  使用VBA中Find搜索内容,当搜索失败时,会抛出异常导致程序无法正常处理  解决方法如下,使用Rng存储,然后用If Not Rng Is Nothing Then判断。

  Set Rng = ThisWorkbook.Sheets(1).Range(colName & firstRow & ":" & colName & lastRow).Find(styleColor)If Not Rng Is Nothing Then’可以找到(这里处理)End If Find循环破除  使用VBA中Find搜索内容,会出现循环搜索的问题,此时,可以使用判断是否回到第一次作为判断,断开循环。   Set Rng = ThisWorkbook.Sheets(1).Range(colName & firstRow & ":" & colName & lastRow).Find(styleColor)If Not Rng Is Nothing ThenrowNum = Rng.RowfirstMatchRow = rowNumWhile rowNum ' 这里写处理逻辑 ' 继续搜索单店指定店铺Set Rng = ThisWorkbook.Sheets(1).Range(colStyleColor & firstRow & ":" & colStyleColor & lastRow).Find(styleColor, after:=Range(colStyleColor & rowNum))If Not Rng Is Nothing ThenrowNum = Rng.RowEnd If ' 如果搜索回到第一个,退出函数 'If firstMatchRow = rowNum ThenrowNum = fasleEnd IfWendEnd If 9. VBA While循环退出循环   While i < 100'这里处理逻辑 'If i = 20 Theni = 100 '利用While的破坏条件退出循环 'End ifWend 10. VBA 字典类型使用   Dim dic As ObjectSet dic = CreateObject("Scripting.Dictionary")If dic.exists(key) = False Thendic.Add key, valEnd If ' 循环读取字典内容 'For Each key In dicval = dic.Item(key)Next ' 移除一个内容 'dic.Remove(key) ' 移除全部内容 'dic.RemoveAll 11. VBA For 循环   For i = 1 To 10MsgBox iNext i 12. VBA 获取最大行号 maxRow = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row 13. VBA If ElseIf   Name = "vba"If Name = "vba" ThenMsgBox "Yes"ElseIf Name = "xxx" ThenMsgBox "No"ElseMsgBox "X"End If 14. VBA 函数定义   ' 1~num求和 'Function getSum(num)Sum = 0For i = 1 To numSum = Sum + iNext i' 返回值为函数同名变量赋值 'getSum = SumEnd Function 15. VBA 函数返回值

VBA中的字典无法作为返回值,此时需要借助全局变量传递返回值

  Public tmpDic As ObjectFunction test()Set tmpDic = CreateObject("Scripting.Dictionary")tmpDic.Add "a", 5End Function 16. VBA 退出Sub或Function

使用exit sub或exit function即可

17. VBA 注释

VBA使用单引号作为注释

18. 复制Sheet ThisWorkbook.Sheets(1).Copy after:=Worksheets(Worksheets.Count) ActiveSheet.Name = "Sheet1备份" 19. 添加Sheet

Worksheets.Add().Name = "Sheet xxx"

未经同意禁止转载! 转载请附带本文原文地址:VBA基本语法整理,首发自 Zjmainstay学习笔记



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有