2014-07-01 2 views
0

커다란 스프레드 시트를 준비했으며 사용자가 스프레드 시트의 입력 값을 변경하면 모듈이 자동으로 실행되어 시트를 업데이트합니다.셀 값 변경시 매크로 자동 실행

Sub Biomass() 
Cells(5, 4).Value = Cells(10, 17).Value 
Cells(4, 1).Value = 0 
Cells(5, 1).Value = 1 
Cells(4, 6).Value = Cells(1, 17).Value * 50000 
Cells(5, 8).Value = Cells(5, 17).Value * 12 
Dim c As Long 
c = Cells(1, 10).Value * Cells(4, 6).Value 
cntr = 0 
For i = 6 To (Cells(1, 7).Value + 4) 
Cells(i, 4).Value = Cells(i - 1, 4).Value * (1 + Cells(11, 17).Value) 
Cells(i, 1).Value = i - 4 
Cells(i - 1, 6).Value = Cells(4, 6).Value - ((i - 5) * c) 
Cells(i, 6).Value = Cells(4, 6).Value - ((i - 4) * c) 
Cells(i - 1, 7).Value = Cells(i - 1, 6).Value/(1 + Cells(1, 2).Value)^(i - 5) 
Cells(i, 7).Value = Cells(i, 6).Value/(1 + Cells(1, 2).Value)^(i - 4) 
Cells(i - 1, 5).Value = (Cells(i - 1, 4).Value/(1 + Cells(1, 2).Value)^(i - 5)) + Cells(i - 2, 5) 
Cells(i, 5).Value = (Cells(i, 4).Value/(1 + Cells(1, 2).Value)^(i - 4)) + Cells(i - 1, 5) 
Cells(i, 8).Value = Cells(i - 1, 8).Value * (1 - Cells(13, 17).Value) 
Cells(i - 1, 9).Value = Cells(i - 1, 8).Value/((1 + Cells(1, 2).Value)^(i - 5)) 
Cells(i, 9).Value = Cells(i, 8).Value/((1 + Cells(1, 2).Value)^(i - 4)) 

Cells(5, 10).Value = Cells(5, 9).Value 
Cells(i, 10).Value = Cells(i - 1, 10).Value + Cells(i, 9).Value 
Cells(i - 1, 11).Value = Cells(14, 17).Value/(1 + Cells(1, 2).Value)^(i - 5) 
Cells(i, 11).Value = Cells(14, 17).Value/(1 + Cells(1, 2).Value)^(i - 4) 
Cells(4, 12).Value = 0 
Cells(i - 1, 12) = Cells(i - 2, 12).Value + Cells(i - 1, 11).Value 
Cells(i, 12).Value = Cells(i - 1, 12).Value + Cells(i, 11).Value 
Cells(i - 1, 13).Value = (Cells(2, 17).Value + Cells(i - 1, 5).Value - Cells(i - 1, 12).Value - Cells(i - 1, 7).Value)/Cells(i - 1, 10).Value 
Cells(i, 13).Value = (Cells(2, 17).Value + Cells(i, 5).Value - Cells(i, 12).Value - Cells(i, 7).Value)/Cells(i, 10).Value 
cntr = cntr + 1 
If cntr = 24 Then Exit For 
Next i 

For g = 5 To (Cells(35, 17).Value + 4) 
Cells(g, 3).Value = Cells(36, 17).Value 
Next g 


cntr2 = 0 
For d = 5 To (Cells(1, 7).Value + 4) 
Cells(d, 2).Value = Cells(21, 17).Value * Cells(d, 8).Value + Cells(d, 3).Value - Cells(d, 4).Value 
cntr2 = cntr2 + 1 
If cntr2 = 25 Then Exit For 
Next d 

cntr3 = 0 
For i = (cntr + 7) To (Cells(1, 7).Value + 4) 
Cells(30, 6).Value = Cells(4, 6).Value * (1 + Cells(1, 4).Value)^30 
c2 = Cells(1, 10).Value * Cells(30, 6).Value 
Cells(30, 4).Value = Cells(5, 4).Value 
Cells(i, 4).Value = Cells(i - 1, 4).Value * (1 + Cells(11, 17).Value) 
Cells(i, 1).Value = i - 4 
Cells(i, 6).Value = Cells(30, 6).Value - ((i - 30) * c2) 
Cells(i - 1, 7).Value = Cells(i - 1, 6).Value/(1 + Cells(1, 2).Value)^(i - 5) 
Cells(i, 7).Value = Cells(i, 6).Value/(1 + Cells(1, 2).Value)^(i - 4) 
Cells(i - 1, 5).Value = (Cells(i - 1, 4).Value/(1 + Cells(1, 2).Value)^(i - 5)) + Cells(i - 2, 5) 
Cells(i, 5).Value = (Cells(i, 4).Value/(1 + Cells(1, 2).Value)^(i - 4)) + Cells(i - 1, 5) 
Cells(30, 8).Value = Cells(5, 8).Value 
Cells(i, 8).Value = Cells(i - 1, 8).Value * (1 - Cells(13, 17).Value) 
Cells(i - 1, 9).Value = Cells(i - 1, 8).Value/((1 + Cells(1, 2).Value)^(i - 5)) 
Cells(i, 9).Value = Cells(i, 8).Value/((1 + Cells(1, 2).Value)^(i - 4)) 

Cells(30, 10).Value = Cells(30, 9).Value + Cells(29, 10).Value 
Cells(i, 10).Value = Cells(i - 1, 10).Value + Cells(i, 9).Value 
Cells(i - 1, 11).Value = Cells(14, 17).Value/(1 + Cells(1, 2).Value)^(i - 5) 
Cells(i, 11).Value = Cells(14, 17).Value/(1 + Cells(1, 2).Value)^(i - 4) 
Cells(i - 1, 12) = Cells(i - 2, 12).Value + Cells(i - 1, 11).Value 
Cells(i, 12).Value = Cells(i - 1, 12).Value + Cells(i, 11).Value 
Cells(i - 1, 13).Value = (Cells(2, 17).Value + Cells(i - 1, 5).Value - Cells(i - 1, 12).Value - Cells(i - 1, 7).Value)/Cells(i - 1, 10).Value 
Cells(i, 13).Value = (Cells(2, 17).Value + Cells(i, 5).Value - Cells(i, 12).Value - Cells(i, 7).Value)/Cells(i, 10).Value 
cntr3 = cntr3 + 1 
If cntr3 = 25 Then Exit For 
Next i 


cntr4 = 0 
For d = 30 To (Cells(1, 7).Value + 4) 
Cells(d, 2).Value = Cells(21, 17).Value * Cells(d, 8).Value + Cells(d, 3).Value - Cells(d, 4).Value 
cntr4 = cntr4 + 1 
If cntr4 = 25 Then Exit For 
Next d 

End Sub 

당신이 볼 수 있듯이, 세포 (10,17) (1,17) (25,17) (5,17) (17, 17) (4,6) (11, 17) (5 , 9) (1,7) (24,17) (5,10)은 하위가 자동으로 실행되어야하는 셀을 변경하는 셀입니다.

답변

0

다음은 문제를 해결할 수 있어야합니다.

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range 

    Set KeyCells = Range("INPUT RANGE") 

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _ 
      Is Nothing Then 
      ' YOUR CODE GOES HERE 
    End If 
End Sub 

입력란을 변경하고 표시된 곳에 코드를 입력하십시오.

+0

입력 내용이 정확히 연속적이지 않습니다. 전체 시트에 퍼져 있습니다. 인접하지 않은 개별 셀을 선택할 수있는 방법이 있습니까? – user3789753

+0

입력의 일부인 모든 셀을 선택하고 명명 된 범위로 설정 한 다음 INPUT RANGE에 명명 된 범위의 이름 만 사용합니다. –

+0

위의 방금 제공 한 개인 서브 루틴 아래에 내 서브 루틴을 중첩 할 수 있습니까? 나는 그것을 시도했지만 컴파일 오류가 발생했습니다 – user3789753

관련 문제