컴퓨터/엑셀

vba 두개의 배열 계산

풍경소리^^ 2020. 11. 24. 18:25

Sub 배열합연습()
    Dim arrSource1()
    Dim arrSource2()
    Dim arrResult()
    Dim i As Long, j As Long
    
    Application.ScreenUpdating = False

 

    arrSource1 = Range("j1", Cells(Rows.Count, "a").End(xlUp))

    arrSource2 = Range("w1", Cells(Rows.Count, "m").End(xlUp))

 

    For j = 1 To UBound(arrSource2, 1) 'i 행 j 열 관행적인 변수
        For i = 1 To UBound(arrSource2, 1) '행 'ubound(arr,1) 행갯수10-현재 열j값1=9+1 '+1 시작이 i=1 이 아니어서 +1로 맞추어줌
            ReDim Preserve arr3(1 To UBound(arrSource1, 1), 1 To UBound(arrSource1, 1))
            If arrSource1(i, j) = 0 Then
                arrResult(i, j) = 0
            Else
                arrResult(i, j) = arrSource2(i, j) / arrSource1(i, j)
            End If
        Next
    Next

    Range("y1").Resize(UBound(arrSource1, 1), UBound(arrSource1, 2)) = arrResult 'Resize(행,열)
    Application.ScreenUpdating = True
    
    Erase arrSource1 '배열이나 개체변수는 프로시져 빠져나가기 전에 초기화 시켜라
    Erase arrSource2 '배열이나 개체변수는 프로시져 빠져나가기 전에 초기화 시켜라
    Erase arrResult '배열이나 개체변수는 프로시져 빠져나가기 전에 초기화 시켜라
End Sub