Visual Basic for Application

weixin_30639719發表於2020-04-05
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 = 4
               If a > 3 Then MsgBox "大於3"
  
      '多行判斷語句:需要使用End If
               If a > 2 Then
                        MsgBox "大於2"
               ElseIf a > 5 Then
                        MsgBox "大於5"
               Else
                        MsgBox "feirup"
              End If
      '(2)IIF判斷語句:
               MsgBox IIf(a > 2, "不錯", "一般")

      '選擇語句:
        'Select Case語句:
               Dim i As Integer
               i = 1
               Select Case i
                        Case Is > 0
                               MsgBox "正數"
                        Case Is < 0
                               MsgBox "負數"
                        Case Else
                               MsgBox "0"
                End Select
     '迴圈語句:
       'DO...LOOP語句:
                Dim t As Integer
                t = 1
                Do
                    t = t + 1
                    If t > 10 Then MsgBox "大於10了"
                Loop

    'While語句:
             Dim rs%
             rs = 2
   
            Do While Cells(rs, 2) <> ""
                 Cells(rs, 3) = "非若"
                 rs = rs + 1
            Loop
    'Until迴圈語句:
            Dim t%
            t = 2
            Do Until Cells(t, 2) = ""
                  Cells(t, 3) = "feiruo"
                   t = t + 1
            Loop
    'for each...next語句:

            Dim rng As Range, n!
            For Each rng In Sheet1.Range("a1:a10")
                  If rng = "A3" Then
                     rng.Interior.ColorIndex = 3
                  Else
                     rng.Interior.ColorIndex = 6
                  End If
            Next
    'for...next語句:
            Dim i!, j!
            For i = 1 To 100 Step 2
                 j = j + 1
            Next i
            MsgBox "j=" & j

     'End與Exit語句:
          '1. Exit Do
          '2.Exit For
          '3.Exit Function
          '4.Exit Sub
            Dim r!
            For r = 2 To 7
              If Sheet1.Cells(r, 1) = "非若" 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 = 3
            MsgBox "i=" & i
             End '直接停止程式
            MsgBox "退出le"
             End Sub

    '跳轉語句:
    'GoTo line無條件轉移程式中指定的程式行
         Dim str$, k!
         k = 1
      line:
         k = k + 1
         If k > 3 Then Exit Sub
              str = InputBox("請輸入使用者名稱!")
         If str <> "admin" Then GoTo line
   '錯誤分支語句:
         Dim i!
        'On Error Resume Next '當遇到錯誤的時候繼續執行下去
         On Error GoTo ERROR:

             For i = 2 To 8
                  Cells(i, 4) = Cells(i, 3) + 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 = 1 To 11 Step 1
   Sheet1.Cells(i, 4) = "=sum(a" & i & ":b" & i & ")"
   Sheet1.Cells(i, 3) = "=COUNTIF(A1:A11,"">12"")"
   Sheet1.Cells(i, 5) = "=sum(INDIRECT(""a1:a10""))"
   Next i

Dim i%
   
   MsgBox Application.WorksheetFunction.CountIf(Range("a1:a10"), 14) '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, 15, 3) Mod 2, "", "")
   End Function

 

 

 

                                                                                                                                        -------非若------

                                                                                                                                          2015.09.24

轉載於:https://www.cnblogs.com/feiruo/p/4836658.html

相關文章