Visual Basic for Application
2024-08-30 12:28:26
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'The note of Visual Basic for Application
'VBA 是由微软开发出来的一种宏语言,寄于对Windows桌面程序功能的扩展,和VB语言结构相似,是VB的子集;
'VBA常见的作用:
'1.规范用户的操作,控制用户的操作行为;
'2.操作界面人性化,方便用户的操作;
'3.多个步骤的手工操作通过执行VBA代码可以迅速的实现;
'4.实现一些VB无法实现的功能;
'5.用VBA制做EXCEL登录系统;
'6.利用VBA可以Excel内轻松开发出功能强大的自动化程序.
'VBA中常见的数据类型:
'1. Integer 整型 %
'2. Single 单精度 !
'3. Double 双精度 #
'4. String 字符串 $
'5. Long 长整型 &
'6. Currency 货币型 @ 'VBA判断语句:
'(1)IF判断语句:
'单行判断语句:不需要使用End If
Dim a As Integer
a =
If a > Then MsgBox "大于3" '多行判断语句:需要使用End If
If a > Then
MsgBox "大于2"
ElseIf a > Then
MsgBox "大于5"
Else
MsgBox "feirup"
End If
'(2)IIF判断语句:
MsgBox IIf(a > , "不错", "一般") '选择语句:
'Select Case语句:
Dim i As Integer
i =
Select Case i
Case Is >
MsgBox "正数"
Case Is <
MsgBox "负数"
Case Else
MsgBox ""
End Select
'循环语句:
'DO...LOOP语句:
Dim t As Integer
t =
Do
t = t +
If t > Then MsgBox "大于10了"
Loop 'While语句:
Dim rs%
rs = Do While Cells(rs, ) <> ""
Cells(rs, ) = "非若"
rs = rs +
Loop
'Until循环语句:
Dim t%
t =
Do Until Cells(t, ) = ""
Cells(t, ) = "feiruo"
t = t +
Loop
'for each...next语句: Dim rng As Range, n!
For Each rng In Sheet1.Range("a1:a10")
If rng = "A3" Then
rng.Interior.ColorIndex =
Else
rng.Interior.ColorIndex =
End If
Next
'for...next语句:
Dim i!, j!
For i = To Step
j = j +
Next i
MsgBox "j=" & j 'End与Exit语句:
'1. Exit Do
'2.Exit For
'3.Exit Function
'4.Exit Sub
Dim r!
For r = To
If Sheet1.Cells(r, ) = "非若" Then
Exit For
End If
Next r
MsgBox "非若(第一个)在第" & r & "行!" Sub button() '1.End
'2.End Function
'3.End If
'4.End Select
'5.End Sub
Dim i!
i =
MsgBox "i=" & i
End '直接停止程序
MsgBox "退出le"
End Sub '跳转语句:
'GoTo line无条件转移程序中指定的程序行
Dim str$, k!
k =
line:
k = k +
If k > Then Exit Sub
str = InputBox("请输入用户名!")
If str <> "admin" Then GoTo line
'错误分支语句:
Dim i!
'On Error Resume Next '当遇到错误的时候继续执行下去
On Error GoTo ERROR: For i = To
Cells(i, ) = Cells(i, ) + Cells(i4)
Next i
MsgBox "运行完毕" ERROR: MsgBox "遇到错误!" 'width语句:对某个对象执行一系列的语句是,不用重复指出对象的名称
'a = Range("a1").Address
'b = Range("a1").Parent.Name
With Range("a1")
a = .Address
b = .Parent.Name
MsgBox a '$A$1
MsgBox b 'Sheet1
End With End Sub
'VBA 函数:
Dim i% For i = To Step
Sheet1.Cells(i, ) = "=sum(a" & i & ":b" & i & ")"
Sheet1.Cells(i, ) = "=COUNTIF(A1:A11,"">12"")"
Sheet1.Cells(i, ) = "=sum(INDIRECT(""a1:a10""))"
Next i Dim i% MsgBox Application.WorksheetFunction.CountIf(Range("a1:a10"), ) '3 统计3的14的个数 等价于: 'MsgBox Application.CountIf(Range("a1:a10"), 14) 'MsgBox WorksheetFunction.CountIf(Range("a1:a10"), 14) MsgBox VBA.Format(Range("b1"), "0.00") MsgBox Format(Range("b1"), "0.00") '自定义函数:
Function sex(rng As Range)
MsgBox sex = IIf(Mid(rng, , ) Mod , "男", "女")
End Function
-------非若------
2015.09.24
最新文章
- ASP.NET Web API自身对CORS的支持:从实例开始
- ps
- Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库
- myeclipse打红叉
- Xcode cannot launch because the device is locked.
- topcoder SRM 625 DIV2 IncrementingSequence
- iOS 并发编程指南
- 电量显示Binding Converter MVVM
- linux下如何优雅的挂载一个外界设备(比如优盘)
- maven学习之一本地(windows)安装
- POJ-3669 Meteor Shower(bfs)
- 学习Mongodb(一)
- DTD Tutorial
- 迪杰斯特拉(Dijkstra)算法
- 【百度地图API】如何在地图上添加标注?——另有:坐标拾取工具+打车费用接口介绍
- Logstash使用grok过滤nginx日志(二)
- 使用Jsoup实现java爬虫(非原创)
- org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
- 路径或文件名中含有中文的jar文件双击启动不了 ->; Java7的Bug?
- 浏览器Hack演示
热门文章
- ActiveMQ学习总结(1)——ActiveMQ快速入门
- Leetcode 41.缺失的第一个正数
- PatentTips – RDMA data transfer in a virtual environment
- 【学QT】 3 - DEBUG集子
- log_archive_dest_1设置报错
- 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方式
- springmvc学习笔记(10)-springmvc注解开发之商品改动功能
- ios 使用Starscream实现websocket简单例子
- 3n+1问题
- Java获取路径中的文件名(正则表达式)