본문 바로가기
Office/Excel

여러 Sheet에 있는 데이터로 작업하는 VBA

by belitino 2012. 1. 2.
출처: http://answers.google.com/answers/threadview/id/737271.html

여러 Sheet에 나뉘어져 있는 데이터들을 합계를 내거나 평균을 내는 등 통계 처리를 해야할 경우에 사용 가능한 Visual Basic for Application 입니다.

여러 시트에 있는 값을 읽어오기 위해서는 Sheets().Range()를 쓰면 됩니다.

다음은 출처에 나와있던 VBA 예제 코드 입니다.

 Sub MakeSummary()
'
' MakeSummary Macro
' Macro created 6/12/2006 by Maniac
'

'
    Sheets("SUMMARY").Select
'   Clear the existing values (if any)
    Range("$A$2:$D$60").Value = ""
'   J tracks the row number on the summary page
'   I tracks the sheet number being processed
    J = 2
    For I = 2 To Sheets.Count
        A$ = Sheets(I).Name
'   Don't process a sheet if its name is "Conversion Table"
'   or if the name is blank.
        If (A$ = "Conversion Table") Then GoTo 10
        If (Sheets(A$).Range("$C$1").Value = "") Then GoTo 10
'   Process the current sheet
        Range("A" + Format(J)).FormulaR1C1 = "='" + A$ + "'!R1C3"
        Range("B" + Format(J)).FormulaR1C1 = "='" + A$ + "'!R2C3"
        Range("C" + Format(J)).FormulaR1C1 = "='" + A$ + "'!R37C8"
        Range("D" + Format(J)).FormulaR1C1 = "='" + A$ + "'!R38C8"
        J = J + 1
10    Next I
End Sub