티스토리 뷰

10가지 핵심

1.msgbox

msgbox("hello")

2.range

range("a1").value = "hello"

msgbox( range("a1").value )

3.concatenate(&)

msgbox( range("a" & 1).value )

4.variable

var_01 = range("a" & 1).value
msgbox(var_01)

5.loop(for)

for i = 1 to 100
    range("a" & i).value="hello"
next

6.if

if range("a" & 1).value = 1 then
    range("b" & 1).value = "true"
else
    range("b" & 1).value = "false"
end if

7.call method

sub printHello()
    msgbox("hello")
endsub
sub test_printHello()
    call printHello
end sub

8.parameter

sub printHello( p_name )
    msgbox("hello" & p_name)
endsub
sub test_printHello()
    call printHello("김경록")
end sub

9.function

function getPlus(p_val_01, p_val_02)
    getPlus = p_val_01 + p_val_02
end function
sub test_getPlus()
    result = getPlus( 10, 20 )
    msgbox(result)
end sub

10.random

rnd_val = int( rnd()*10 ) + 1

Array(배열) 선언과 출력

//Excel vba array(배열) 선언하고 for each로 출력하기
Sub test_array()

    ar = Array(4, 5, 8, 9, 20)

    For Each element In ar
        Debug.Print element
    Next

End Sub

정렬

Sub 정렬()
    Worksheets("filter").Sort.SortFields.Clear
    Worksheets("filter").Sort.SortFields.Add2 Key:=Range("H9"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("filter").Sort
        .SetRange Range("A9").CurrentRegion
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub 정렬()
    Range("a8").CurrentRegion.Sort key1:=Range("G9"), order1:=xlAscending, _
    key2:=Range("H9"), order2:=xlAscending, _
    key3:=Range("R9"), order3:=xlAscending, Header:=xlYes
End Sub

글자 나누기 Split()

Sub main()

    대상값 = Range("a1").Value
    ar = Split(대상값, ",")

    Range("c1").Value = ar(0)
    Range("c2").Value = ar(1)

End Sub

결과
abcd
efgh


Excel VBA 파일 열어서 필요한 부분 복사해오고 파일 닫기

Sub fileOpenCopyPasteClose()
    '열고
    Workbooks.Open "c:\practice\hello.xlsx"

    '카피하고
    Workbooks("hello.xlsx").Sheets("Sheet1").Range("a1:c100").Copy


    '붙혀넣고
    Workbooks("열고_카피하고_붙여넣고_닫고.xlsm").Sheets("Sheet1").Range("a1").PasteSpecial


    '닫고
    Workbooks("hello.xlsx").Close
End Sub

파일은 c:\practice\hello.xlsx입니다.

범위는 "Sheet1"의 "a1:c100"까지 입니다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함