VBA 기초 지식 정리

12073 단어 vba
             ,   Excel       ,        ,      ,            ,      ,        ,VBA     。

1.  
    1.Dim <> As <>
    2.  
                           。
                  ,  (.),   (!)   @,&,$,#。
                   255   。
            Visual Basic           。
    3. 
        Sub var()
            Dim str As String
            str = "string"
            MsgBox (str)
        End Sub
2.  
    1.Const <> As <> = <>
    2.  
                           。
                      ,  (.),   (!)   @,&,$,#。
                   255   。
            Visual Basic           。
    3.
        Sub const_var()
            Const MyInteger As Integer = 720
            MsgBox (MyInteger)
        End Sub
3.  
    1.              ,             
    2.  
                 5,            ,     6  
                 
                        ,      。
    2.    
          
            1. Dim arr1()  
            2. Dim arr2(5)
            3. arr3 = Array("apple","Orange","Grapes")
          
            Dim arr(5)
            arr(0) = "1"
            arr(2) = 100 
            arr(3) = 2.45
            arr(4) = #10/07/2013#   ’  
            arr(5) = #12.45 PM#  '  
    3.    
          
            1. Dim arr(2,3)
          
            arr(0,0) = "Apple" 
            arr(0,1) = "Orange"
            arr(1,0) = "cucumber"
    4.    
        LBound(ArrayName[,dimension])
            '           ,   LBound     。
            MsgBox ("Line 1 : " & LBound(Array(5, 2, 3)))
        UBound(ArrayName[,dimension])
            '           。
            MsgBox ("Line 1 : " & UBound(Array(5, 2, 3)))
        Split(expression[,delimiter[,count[,compare]]])
            '      ,                  。
            Split("Red $ Blue $ Yellow","$")
        Join(List[,delimiter])
            '                    
            MsgBox ("Line 1" & " is :" & Join(Array("Red", "Blue", "Yellow"), "---"))
        Filter(inputstrings,value[,include[,compare]])
            '            ,                     。
        IsArray(variablename)
            '       ,                
            msgbox("Line 1 : " & IsArray(Array(1,2,3)))
            msgbox("Line 1 : " & IsArray("sdfadd"))

4.ReDim  
    ’                      
    ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
    Sub Constant_demo_Click()
       Dim a() as variant
       i = 0
       redim a(5)
       a(0) = "XYZ"
       a(1) = 41.25
       a(2) = 22

       REDIM PRESERVE a(7)
       For i = 3 to 7
       a(i) = i
       Next

       'to Fetch the output
       For i = 0 to ubound(a)
          Msgbox a(i)
       Next
    End Sub

5.   
    1.     
        + :        
        - :        
        * :        
        / :        
        % :     ,        
        ^ :      
    2.     
        = :              。   ,      。   
        <> :               。      ,     。   
        > :                   。   ,      。  
        < :                   。   ,      。  
        >= :                      。    ,      。 
        <= :                      。   ,      。  
    3.     
        AND :        ,      。
        OR :               ,     。
        NOT :              。       ,                。
        XOR :                 True,    True。
    4.     
        & :      
            1 & 2 = 12
            "1" & "2" = 12
        + :          ,     
            1 + 2 = 3
            "1" + "2" = 12

6.        
    1.Range
        Range("A1")  '  A1   
        Range("A1:A9")    '  A1-A9   
    2.[]
        [A1]    '  A1   
        [A1:A9]     '  A1 A9   
    3.Cells(   ,   )
        Cells(1,1)   '            

7.VBA       
    1.       
        Sub alert()
            'A1 = Sheets(1).[a1]    '        (      1) A1     X
            'A1 = Sheets("   1").[a1]
            'A1 = Sheets(1).Cells(1, 1)
            'A1 = Sheets("   1").Cells(1, 1)
            A1Range("A1").Value    '       A1  
            MsgBox (A1)   '     
        End Sub
    1.     
        Sub setVal()
            Range("A1").Value = 100   '  VBAProject   '     '
        End Sub
    2.         
        Sub setColor()
            Range("B3").Interior.ColorIndex = 3   '  1-56,  56   
        End Sub
    3.      、  
        Sub cut()
            Range("B3").copy Range("C3")   ' B3  ,   C3   
        End Sub
    4.      
        Sub cut()
            Range("A1").cut Range("B3")  ' A1  ,   B3   
        End Sub

8.      
    if then   
        Sub if_test()
            B3 = Range("B3").Value
            If B3 > 10 Then
                MsgBox ("  10")    '      ,      
            ElseIf B3 < 10 Then
                MsgBox ("  10")
            Else
                MsgBox ("aaaa")
            End If
            MsgBox (B3)
        End Sub
9.      
    Select Case   
        Sub select_test()
            B3 = Range("B3").Value
            Select Case B3
            Case 1 To 10
                msg = "1 10  "
            Case 11 To 20
                msg = "11 20  "
            Case 21, 22
                msg = "21,22  "
            Case Else
                msg = "    "
            End Select
            MsgBox (msg)
        End Sub
10.    
    for next   
        Sub for_test()
            For i = 1 To 10 Step 1
                If i > 5 Then    
                    Exit For    '  for  
                End If
                Range("D" & i).Value = i    '     D1-D10
            Next i
        End Sub

    for each   :              
        Sub for_ecah_test()
            fruits = Array("  ", "  ", "  ")
            For Each Item In fruits
                MsgBox (Item)
            Next
        End Sub

    do  while  loop 
        Sub do_while_loop_test()
           Do While i < 5    '      ,        
              If i > 3 Then
                  MsgBox ("    ")
                  Exit Do
              End If
              i = i + 1
              MsgBox ("The value of i is : " & i)
           Loop
        End Sub

        Sub do_while_loop_test()
           Do 
              If i > 3 Then
                  MsgBox ("    ")
                  Exit Do
              End If
              i = i + 1
              MsgBox ("The value of i is : " & i)
           Loop While i < 5    '      ,        
        End Sub

11.sub   function    
                ,                 。
           call      。
            Sub End Sub   。
          :
        Function function_test(ByVal col, ByVal start_row, ByVal end_row)
            total = 0
                For i = start_row To end_row Step 1
                    total = total + Range(col & i).Value
                Next i
            function_test = total
        End Function
        Sub call_function()
            MsgBox ("   :" & function_test("A", 1, 5))
        End Sub
          
        Sub test(name As String, age As Integer)
           MsgBox ("  :" & name & "   :" & age)
        End Sub
        Function call_sub()
            test "aaa", 22
        End Function

12.     
    InStr([start,]string1,string2[,compare]) 
        '                    ,      。            。
        MsgBox ("Line 1 : " & InStr(1, “safdfasdf”, "s")) 
    InStrRev(string1,string2[,start,[compare]])
        '                       。      
        msgbox("Line 1 : " & InStrRev("asdfasdf","s",10))
    Lcase(String)
        '                 
        msgbox("Line 1 : " & LCase("DJUEG"))
    UCase(String)
        '                 
        msgbox("Line 1 : " & LCase("ioek"))
    Left(String, Length)
        '                
        msgbox("Line 1 : " & Left("adfasdf",2))
    Right(String, Length)
        '                
        msgbox("Line 1 : " & Right("adfasdf",2))
    Mid(String,start[,Length])
        ’                 
        msgbox("Line 1 : " & Mid("adfsd",2))
    Ltrim(String)
        '          。
        msgbox "After Ltrim : " & LTrim("   adfasdfsd")
    RTrim(String)
        '          
        msgbox "After Ltrim : " & RTrim("adfasdfsd  ")
    Trim(String)
        '                   
        msgbox "After Ltrim : " & RTrim("   adfasdfsd  ")
    Len(String)
        '            ,    
        msgbox("Length of var1 is : " & Len("sdf sdfsd  "))
    space(number)
        '             
        msgbox("aaa" & Space(2)& "bbb")
    StrComp(string1,string2[,compare])
        '          ,       。
        msgbox("Line 1 :" & StrComp("Microsoft","Microsoft"))
    Replace ( string1, find, replacement, [start, [count, [compare]]] )
        '                  。
        msgbox("Line 1 :" & Replace("alphabet", "a", "e", 1, 1))
    String(number,character)
        '                 
        msgbox("Line 1 :" & String(3,"$"))
    StrReverse(string)
        '        
         msgbox("Line 1 : " & StrReverse("VBSCRIPT"))

13.    
    date()
        '         
        msgbox("The Value of a : " & a)
    cdate(date)
        '                   
        MsgBox ("The Value of a : " & CDate("Jan 01 2020"))
    DateAdd(interval,number,date)
        '                   
        msgbox("Line 1 : " &DateAdd("h",1,"01-Jan-2013 12:00:00"))
    DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])
        '                。
        msgbox("Line 2 : " &DateDiff("q","01-Jan-09 00:00:00","01-Jan-10 23:59:00"))
    DatePart(interval,date[,firstdayofweek[,firstweekofyear]])
        '           
        msgbox("Line 4 : " & DatePart("m","2013-01-15"))
    Day(date)
        '  1 31     ,         
        msgbox(Day("2018-06-30"))
    DateSerial(year,month,day)
        '       ,          
        msgbox(DateSerial(2018,5,10))
    FormatDateTime(date,format)
        '                 
        msgbox("Line 5 : " & FormatDateTime("2018-08-15 20:25",4))
    IsDate(expression)
        '       ,          
           msgbox("Line 1 : " & IsDate("Nov 03, 1950"))
    Month(date)
        '  1 12     ,         。
        msgbox("        :"&Month("2018-06-30"))
    Year(date)
        '                
        msgbox(Year("2018-06-30"))
    MonthName(month[,toabbreviate])
        '          。
        msgbox("Line 1 : " & MonthName(01,True))
        msgbox("Line 2 : " & MonthName(01,false))
    Weekday(date[,firstdayofweek])
        '     1 7   ,          。
        msgbox("Line 1: " & Weekday("2013-05-16",1))
    WeekdayName(weekday[,abbreviate[,firstdayofweek]])
        '            
        msgbox("Line 2 : " &WeekdayName(2,True))
        msgbox("Line 3 : " &WeekdayName(1,False))
        msgbox("Line 4 : " &WeekdayName(2,True,0))
        msgbox("Line 5 : " &WeekdayName(1,False,1))

좋은 웹페이지 즐겨찾기