VBA’s Help System
F1->搜索按钮右侧箭头->开发人员参考
Object Model Map
The Object Browser。在Visual Basic Editor里按F2。
Auto List Members。输入时的自动补全功能。
VBA Language Elements
Sub和Function
区别是Function过程可以返回一个值给调用的过程。
1 2 3 4 5 6 7 8 9 10 11
Sub ShowMessage() MsgBox "That's all folks!" EndSub Function CubeRoot(number) AsDouble CubeRoot = number ^ (1 / 3) '返回值需与函数同名 EndFunction Sub Test() Call ShowMessage() Dim Tmp AsDouble Tmp = CubeRoot(2) EndSub
Dim King AsString, Kong AsLong Dim Mouthful asByte Dim Julian AsBoolean Dim Unmarried AsSingle Dim Trouble AsDouble Dim WindingRoad AsLong Dim Blind AsDate Public Nuisance Private FirstClass Static Cling, Electricity Dim BaseballCards AsNew Collection Dim DentalFloss AsString
Dim MyArray (1 To 100) As Integer // 100 Integers Dim MyArray (0 To 100) As Integer // 101 Integers Dim MyArray (100) As Integer // 101 Integers Option Base 1 // Set 1 to be the default lower index Dim MyArray (100) As Integer // 100 Integers
Multidimensional arrays
1 2
Dim MyArray (1To9, 1To9) AsInteger MyArray (3, 4) = 125
Dynamic arrays
1 2 3
Dim MyArray () As Integer ReDim MyArray (1 To NumElements) // Destroy old values in array ReDim Perserve MyArray (1 To NumElements) // Keep old values in array
Flow Control
GoTo Statement
1 2 3 4 5 6 7 8 9
Sub GoToDemo() UserName = InputBox("Enter Your Name: ") If UserName <> "Bill Gates"ThenGoTo WrongName MsgBox ("Welcome Bill...") ' ................... ExitSub WrongName: MsgBox "Sorry. Only Bill Gates can run this." EndSub
If-Then Structure
1 2 3 4 5 6 7 8 9 10 11
Sub GreetMe() Dim Msg AsString If Time < 0.5Then Msg = "Morning" ElseIf Time >= 0.5And Time < 0.75Then Msg = "Afternoon" Else Msg = "Evening" EndIf MsgBox "Good " & Msg EndSub
Sub ExitForDemo() Dim MaxVal AsDouble Dim Row AsLong MaxVal = WorksheetFunction.Max(Range("A:A")) For Row = 1To Rows.Count Step1 If Range("A1").Offset(Row-1, 0).Value = MaxVal Then Range("A1").Offset(Row-1, 0).Activate MsgBox "Max value is in Row " & Row ExitFor EndIf Next Row EndSub
Do-While Loop
1 2 3 4 5 6
Sub DoLoopWhileDemo() Do ActiveCell.Value = ActiveCell.Value * 2 ActiveCell.Offset(1, 0).Select LoopWhile ActiveCell.Valud <> Empty EndSub
Do-Until Loop
1 2 3 4 5 6
Sub DoLoopUntilDemo() Do ActiveCell.Value = ActiveCell.Value * 2 ActiveCell.Offset(1, 0).Select LoopUntil IsEmpty(ActiveCell.Value) EndSub
Looping Through a Collection
1 2 3 4 5 6 7 8 9 10
Sub ChangeSign() Dim Cell As Range ForEach Cell In Range("A1:E50") IfNot Cell.HasFormula Then If IsNumeric(Cell.Value) Then Cell.Value = Cell.Value * -1 EndIf EndIf Next Cell EndSub
1 2 3 4 5 6
Sub ChangeCharts() Dim Cht As ChartObject ForEach Cht In Sheets("Sheet1").ChartObjects Cht.Chart.ChartType = xlLine Next Cht EndSub
Example
删除 PowerPoint 2010 中的空文本框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sub RemoveEmptyTextBoxes() Dim SlideObj As Slide Dim ShapeObj As Shape Dim ShapeIndex AsInteger ForEach SlideObj In ActivePresentation.Slides For ShapeIndex = SlideObj.Shapes.Count To1Step-1 Set ShapeObj = SlideObj.Shapes(ShapeIndex) If ShapeObj.Type = msoTextBox Then If Trim(ShapeObj.TextFrame.TextRange.Text) = ""Then ShapeObj.Delete EndIf EndIf Next ShapeIndex Next SlideObj EndSub