VBA学习笔记
更新时间:2024-01-05 12:28:01 阅读量: 教育文库 文档下载
- vba学会要多久推荐度:
- 相关推荐
Sub sss()
Select Case Range(\Case Is = 1
MsgBox \代表入门\Case \
MsgBox (\代表基础\Case \
MsgBox (\代表熟悉\Case \
MsgBox (\代表精通\Case \
MsgBox (\代表专家级\End Select End Sub
Sub save()
Dim iResponse As Integer
iResponse = MsgBox(\If iResponse = vbYes Then
Application.Dialogs(xlDialogSaveAs).Show End If End Sub
Sub been()
Dim icount As Integer
For icount = 10 To 1 Step -2 Beep
MsgBox \Next End Sub
Public Sub ListOfName() Dim i As Integer
Dim iName() As String Dim iCount As Integer Dim iResponse As Integer
iResponse = vbYes
Do While iResponse = vbYes iCount = iCount + 1
ReDim Preserve iName(iCount) As String iName(iCount) = InputBox(\请输入名字:\If iName(iCount) = \
iResponse = MsgBox(\你想继续加入名字吗?\ If iResponse = vbYes Then
iName(iCount) = InputBox(\请输入名字:\ End If End If Loop
For i = 1 To iCount - 1
MsgBox (\Next End Sub
Public Sub ReducePrice() Dim i As Range
For Each i In ThisWorkbook.Worksheets(\i.Value = i.Value - 5 If i.Value <= 0 Then
i.Font.Color = RGB(255, 0, 0) MsgBox \数字小于等于0\ End If Next i End Sub
Public Sub CompareAA() Dim i As Integer Dim j As Integer Dim m As Integer
For i = 2 To Sheets.Count
For j = 4 To Sheets(i).Range(\
m = Sheets(\
If Sheets(i).Cells(j, 6) = Sheets(\).Cells(m, 1) And Sheets(i).Cells(j, 3) = Sheets(\) = Sheets(\Sheets(i).Cells(j, 6).Interior.ColorIndex = 3 Sheets(i).Cells(j, 1).Interior.ColorIndex = 3 Sheets(i).Cells(j, 2).Interior.ColorIndex = 3 Sheets(i).Cells(j, 3).Interior.ColorIndex = 3 Sheets(i).Cells(j, 4).Interior.ColorIndex = 3 Sheets(i).Cells(j, 5).Interior.ColorIndex = 3 End If Next m Next j Next i End Sub
Sub Hebing() Dim i As Integer
For i = 1 To Range(\Application.DisplayAlerts = False
If Cells(i, 6) <> \Range(Cells(i, 8), Cells(i + 1, 8)).Merge Range(Cells(i, 9), Cells(i + 1, 9)).Merge Range(Cells(i, 10), Cells(i + 1, 10)).Merge Range(Cells(i, 11), Cells(i + 1, 11)).Merge Range(Cells(i, 12), Cells(i + 1, 12)).Merge Range(Cells(i, 13), Cells(i + 1, 13)).Merge End If
Application.DisplayAlerts = True Next i End Sub
Sub GetValue()
Dim Sheetname As String Dim i As Integer Dim j As String Dim k As Integer Dim a As Integer
j = Range(\For i = 7 To j
If Cells(i, 4) <> \
Sheetname = Mid(Cells(i, 4), 9, 12) Cells(i, 1) =
Workbooks(ActiveWorkbook.Name).Worksheets(Sheetname).Range(\End If Next
Application.DisplayAlerts = False For k = 7 To j For a = 1 To j - k
If Cells(k, 4) = Cells(k + a, 4) And Cells(k, 4) <> Cells(k + a + 1, 4) Then Range(Cells(k, 1), Cells(k + a, 1)).Merge End If Next Next
Application.DisplayAlerts = True End Sub
Public Sub EnterName() Dim iname As String iname = \
Do While iname = \ If iname = \
iname = MsgBox(\ If iname = vbYes Then
iname = InputBox(\ End If End If Loop End Sub
Sub WorkbookExample()
Dim WorkbookExample As Workbook
Set WorkbookExample = Workbooks.Add // 对象变量一定要用Set WorkbookExample.Worksheets(\WorkbookExample.SaveAs \WorkbookExample.Close End Sub
Sub test()
Dim x As Range
For Each x In ThisWorkbook.Worksheets(\x.Value = x.Value + 10 Next End Sub
Sub BoldEveryOther() Dim iCounter As Integer
For iCounter = 1 To ThisWorkbook.Sheets(\基础数据\Step 2
ThisWorkbook.Sheets(\基础数据\True Next End Sub
Sub SelectRange()
ThisWorkbook.Worksheets(\基础数据\ActiveCell.CurrentRegion.Select
MsgBox \End Sub
Public Sub CompareAA() Dim i As Integer Dim j As Integer Dim m As Integer Dim n As Integer
Dim x As String Dim k As Workbook
x = InputBox(\请输入港盛资料excel表名:\Set k = Workbooks(x)
m = Application.InputBox(\请输入该月起始行号:\n = Application.InputBox(\请输入该月结束行号:\
For i = 7 To k.Worksheets(\船舶明细\For j = m To n
If k.Worksheets(1).Cells(i, 4) = ThisWorkbook.Worksheets(\k.Worksheets(1).Cells(i, 4).Interior.ColorIndex = 4 End If Next j Next i End Sub
关于代码规范:
1. 变量名字前缀,取名字的时候注意可读性 2. 注意代码的位置对齐 3. 注释,这个决定将来维护
Public Sub test()
Dim FileName As String
FileName = Application.GetOpenFilename(\文件,*.xls\If FileName <> \
Workbooks.Open FileName, 0, 1
Range(\0)
ActiveWorkbook.Close 0 End If End Sub
Sub test1()
fileToOpen = Application _
.GetOpenFilename(\If fileToOpen <> False Then MsgBox \End If End Sub
Private Sub CommandButton1_Click() Dim nPath$, nApp
nPath = \ '查询文件路径,自己更新 Set nApp = GetObject(nPath)
nApp.Sheets(1).Range(\nApp.Close
Set nApp = Nothing End Sub
Public Sub MSC()
FileToOpen = Application.GetOpenFilename(\select the files...\If IsArray(FileToOpen) = 0 Then MsgBox \没有选择文件\
MsgBox \End If End Sub
复制未开打文件的内容:
Private Sub test() Dim nPath$, nApp
nPath = \查询文件路径,自己更新 Set nApp = GetObject(nPath)
nApp.Sheets(1).Range(\nApp.Close
Set nApp = Nothing End Sub
获取多个未打开表名和路径
Sub zldccmx() Dim Fp, Dic, Fn
Set Dic = CreateObject(\ Do
Fp = Application.GetOpenFilename(\文件(*.xls), *.xl*\请选择一个目录\
If Fp = False Then Exit Do Fp = Left(Fp, InStrRev(Fp, \ Fn = Dir(Fp & \
Do While Fn <> \ Dic(Fp & Fn) = \ Fn = Dir Loop Loop
If Dic.Count > 0 Then
With Sheets.Add.[a1].Resize(Dic.Count, 1)
.Value = WorksheetFunction.Transpose(Dic.keys) .Sort [a1] End With End If End Sub
Option Explicit
Dim arr(), l As Integer
Function fld(Path As String) Dim fso As Object Dim f As Object Dim fd As Object Dim subf As Object
Set fso = CreateObject(\ Set fd = fso.GetFolder(Path) For Each f In fd.Files l = l + 1
ReDim Preserve arr(1 To l) arr(l) = f.Path Next
For Each subf In fd.SubFolders fld (subf.Path) Next End Function Sub test()
Dim sh As Worksheet, Myname$
Dim brr(1 To 60000, 1 To 5), crr As Variant
Dim n As Integer, i As Integer, j As Integer, wn As String, k As Integer Set sh = ActiveSheet fld ThisWorkbook.Path
Application.ScreenUpdating = False
Sheet6.Range(\ For i = 1 To UBound(arr) Myname = Dir(arr(i))
wn = Replace(Myname, \ If InStr(wn, \结算\
If Myname <> ThisWorkbook.Name Then With GetObject(arr(i))
If .Sheets(\表三甲\
crr = .Sheets(\表三甲\& .Sheets(\表三甲\
For j = 1 To UBound(crr) If crr(j, 1) <> \ n = n + 1
brr(n, 1) = wn: brr(n, 2) = crr(j, 1) For k = 3 To 5
brr(n, k) = crr(j, k) Next End If Next
.Close False End With End If End If nn:
Next
Erase arr l = 0
Sheet6.[a2].Resize(n, 5).Value = brr Application.ScreenUpdating = True End Sub
正在阅读:
VBA学习笔记01-05
2第二章 中学生的心理发展与教育03-07
(深圳中企智业)2016-2021年饲料级磷酸氢钙行业投资机会与风险防范 -12-06
2013高考物理一轮总复习:第十章实验十一知能优化演练06-09
仪器仪表英文对照04-07
数字电路相关面试题04-10
铸造练习题09-08
防溺水作文400字02-06
北京大学艺术品投资与经营高级研修班10-06
魅族mx3和小米3对比 - 图文03-02
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 笔记
- 学习
- VBA
- 五年级智力大冲浪试卷
- “礼让斑马线,文明过马路”活动总结 - 图文
- 浅析如何对房地产项目开发的成本进行控制
- 《市政府工作报告》座谈会发言稿
- 高考诗歌鉴赏讲义
- 人教版二年级语文下册第25课《羿射九日》说课稿
- 2016有理数单元基础练习卷
- “十三五”重点项目-发动机项目可行性研究报告 - 图文
- 宝鸡4S店可研报告电
- 中国笔头行业市场前景分析预测年度报告(目录) - 图文
- 青春期教育讲座
- 试卷分析教案
- 组合逻辑电路单元测试题
- 危险品物流项目可行性研究报告
- “四项排查”中基层组织薄弱环节汇报材料
- 相互作用与共点力的平衡专题复习
- 新视野大学英语1(第三版)Sample essay
- 国际著名设计竞赛
- 中国发动机气缸体市场发展研究及投资前景报告(目录) - 图文
- 工作五步法具体内容