2014-07-01 56 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 

只需更改INPUT RANGE,然后在我标记的地方输入您的代码。

+0

我的输入不完全相邻..他们遍布整个表...有没有一种方法可以选择单个单元格不在一起? – user3789753

+0

我会选择所有属于输入部分的单元格,并将它们设置为命名范围,然后仅使用INPUT RANGE中指定范围的名称。 –

+0

是否可以将我的子例程嵌套在上面刚刚提供的私有子例程中?我尝试过,但我得到一个编译错误 – user3789753

相关问题