Office VBA 参考(官方)

[https://docs.microsoft.com/zh-cn/office/vba/api/overview/](https://docs.microsoft.com/zh-cn/office/vba/api/overview/)

工作簿和工作表

新建工作簿

  1. Sub CreateNewWorkbook()
  2. Workbooks.Add
  3. ActiveWorkbook.SaveAs ThisWorkbook.Path & "\NewBook1.xlsx"
  4. End Sub

导入文本中的数据

  1. Sub UserOpenText()
  2. Workbooks.OpenText _
  3. Filename:=ThisWorkbook.Path & "\Students.txt", _
  4. StartRow:=2, _
  5. DataType:=xlDelimited, _
  6. Other:=True, _
  7. OtherChar:="|", _
  8. FieldInfo:=Array(Array(1, 9), Array(2, 1), _
  9. Array(3, 1), Array(4, 5))
  10. ActiveWorkbook.Sheets(1).Columns("A:C").AutoFit
  11. End Sub

保存工作簿

  1. Sub SaveThisBook()
  2. ThisWorkbook.Save
  3. End Sub
  4. Sub SaveAsWorkbook()
  5. ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & _
  6. "\Backup.xlsm", Password:="123"
  7. End Sub
  8. ' 复制
  9. Sub SaveWorkbookCopy()
  10. ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "\副本.xlsm"
  11. End Sub

获取工作簿文件信息

  1. '获取工作簿文件信息
  2. Sub wbMsg()
  3. Range("b2") = ThisWorkbook.Name '工作簿名称
  4. Range("b3") = ThisWorkbook.Path '工作簿文件所在的路径
  5. Range("b4") = ThisWorkbook.FullName '获得带路径的工作签名称
  6. End Sub

添加新工作表

  1. Sub AddWorksheet()
  2. Sheets.Add After:=Sheets(Sheets.Count)
  3. ActiveSheet.Name = "新添工作表"
  4. End Sub
  5. Sub AddNewWorksheet()
  6. Dim wksSht As Worksheet
  7. With ThisWorkbook
  8. Set wksSht = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
  9. End With
  10. wksSht.Name = "新添工作表2"
  11. Set wksSht = Nothing
  12. End Sub
  13. Sub DeleteWorksheet()
  14. Application.DisplayAlerts = False
  15. Dim wksSht As Worksheet
  16. For Each wksSht In Worksheets
  17. If wksSht.Name <> "Sheet1" Then wksSht.Delete
  18. Next
  19. Set wksSht = Nothing
  20. Application.DisplayAlerts = True
  21. End Sub

防止更改工作表的名称

  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2. If Me.Name <> "Excel Home" Then Me.Name = "Excel Home"
  3. End Sub

判断工作簿中是否存在指定名称的工作表

  1. Function blnSheetExist(ByVal strShtName As String) As Boolean
  2. Dim wksSht As Worksheet
  3. On Error Resume Next
  4. Set wksSht = Worksheets(strShtName)
  5. If Err.Number = 0 Then blnSheetExist = True
  6. Set wksSht = Nothing
  7. End Function

按名称排序工作表

  1. Sub SortShtByName()
  2. Dim intSheetCount As Integer
  3. Dim i As Integer, j As Integer
  4. Dim strName As String, astrSheet() As String
  5. Application.ScreenUpdating = False
  6. intSheetCount = Worksheets.Count
  7. ReDim astrSheet(1 To intSheetCount)
  8. For i = 1 To intSheetCount
  9. astrSheet(i) = Worksheets(i).Name
  10. Next i
  11. For i = 1 To intSheetCount - 1
  12. For j = i + 1 To intSheetCount
  13. If astrSheet(i) < astrSheet(j) Then
  14. strName = astrSheet(i)
  15. astrSheet(i) = astrSheet(j)
  16. astrSheet(j) = strName
  17. End If
  18. Next j
  19. Next i
  20. For i = 1 To intSheetCount
  21. Worksheets(astrSheet(i)).Move Before:=Worksheets(1)
  22. Next i
  23. Application.ScreenUpdating = True
  24. End Sub
  25. Sub SortByNumber()
  26. Dim i As Integer, intSheetCount As Integer
  27. Dim aintArr1() As Integer, aintArr2() As Integer
  28. Application.ScreenUpdating = False
  29. intSheetCount = Worksheets.Count
  30. ReDim aintArr1(1 To intSheetCount)
  31. ReDim aintArr2(1 To intSheetCount)
  32. For i = 1 To intSheetCount
  33. aintArr1(i) = Val(Worksheets(i).Name)
  34. Next i
  35. For i = 1 To intSheetCount
  36. aintArr2(i) = Application.WorksheetFunction.Large(aintArr1, i)
  37. Next i
  38. For i = 1 To intSheetCount
  39. Worksheets(aintArr2(i) & "月份").Move Before:=Worksheets(1)
  40. Next i
  41. Application.ScreenUpdating = True
  42. End Sub

消息框

  1. ' 显示简单的信息提示
  2. Sub SimpleMsgBox()
  3. MsgBox "Hello VBA!"
  4. End Sub
  5. ' 个性化信息框
  6. Sub CustomMsgbox()
  7. MsgBox Prompt:="欢迎光临 Excel Home!", _
  8. Buttons:=vbOKCancel + vbInformation + vbDefaultButton2, _
  9. Title:="Excel Home"
  10. End Sub
  11. ' 获取消息框的返回值
  12. Sub ReturnValueForMsgbox()
  13. Dim intMsg As Integer
  14. intMsg = MsgBox("文件即将关闭,是否保存?", _
  15. vbYesNoCancel + vbQuestion)
  16. Select Case intMsg
  17. Case vbYes
  18. ThisWorkbook.Save
  19. Case vbNo
  20. ThisWorkbook.Saved = True
  21. Case vbCancel
  22. Exit Sub
  23. End Select
  24. ThisWorkbook.Close
  25. End Sub
  26. ' 自动延时关闭的消息框

数据输入

  1. Sub SimpleInput()
  2. Dim strInput As String
  3. strInput = InputBox("请输入邮政编码:", "邮政编码", "100001")
  4. If StrPtr(strInput) <> 0 Then
  5. If IsNumeric(Trim(strInput)) = True Then
  6. If Len(Trim(strInput)) = 6 Then
  7. Cells(1, 1) = strInput
  8. Else
  9. MsgBox "邮政编码必须是6位!"
  10. End If
  11. Else
  12. MsgBox "邮政编码必须是数值!"
  13. End If
  14. Else
  15. MsgBox "你已放弃了输入!"
  16. End If
  17. End Sub

限制只能输入数字

  1. Sub InputNumber()
  2. Dim vntInput As Variant
  3. vntInput = Application.InputBox(Prompt:="请输入数字:", Type:=1)
  4. If TypeName(vntInput) = "Boolean" Then
  5. MsgBox "你已取消了输入!"
  6. Else
  7. Cells(1, 1) = vntInput
  8. End If
  9. End Sub

获取单元格区域地址

  1. Sub RngInput()
  2. Dim rngRange As Range
  3. On Error GoTo line
  4. Set rngRange = Application.InputBox(Prompt:="区域选择:", Type:=8)
  5. rngRange.Interior.Color = 255
  6. line:
  7. End Sub

数据交换

  1. Sub uRngTomRng()
  2. Dim avntNum() As Variant, avntDate() As Variant
  3. avntNum = Range("a1:a11")
  4. avntDate = Range("b1:b11")
  5. Range("a1:a11") = avntDate()
  6. Range("b1:b11") = avntNum()
  7. End Sub