VBA-如果行隐藏在下面,则求和

时间:2019-02-17 20:14:07

标签: excel vba excel-vba-mac

我正在制作一个组织图,每个级别都附加有值。我已经有一个简单的隐藏行VBA。我该如何添加它,例如

如果公司A的值(例如J11)=在H14,J14和L14中显示的公司1、2和3的值

然后,当第14行隐藏时,则J11 = sum(H14,J14,L14)

当第14行可见时,则J11 = 0

到目前为止,这就是我隐藏/取消隐藏行的方法。

Sub sbHideAll()

Rows("10:25").EntireRow.Hidden = True

End Sub

Sub sbShowAll()

Call sbHideAll

Rows("10:25").EntireRow.Hidden = False

End Sub

Sub sbShowGUCL()

Call sbHideAll

Rows("10:11").EntireRow.Hidden = False

End Sub

1 个答案:

答案 0 :(得分:0)

You can't do it directly but you can get the value of the visible cells.

So take the value of the range like this:

Application.WorksheetFunction.Sum(Union(Range("H13:H100"), Range("J13:J100"), Range("L13:L100")))

then take the value of the visible cells using .Rows.SpecialCells(xlCellTypeVisible) like this:

Application.WorksheetFunction.Sum(Union(Range("H13:H100").Rows.SpecialCells(xlCellTypeVisible), Range("J13:J100").Rows.SpecialCells(xlCellTypeVisible), Range("L13:L100").Rows.SpecialCells(xlCellTypeVisible)))

Then minus one from the other

You can either assign each to a variable or just plop it straight into J11 like so:

Range("J11").Formula = Application.WorksheetFunction.Sum(Union(Range("H13:H100"), Range("J13:J100"), Range("L13:L100"))) - Application.WorksheetFunction.Sum(Union(Range("H13:H100").Rows.SpecialCells(xlCellTypeVisible), Range("J13:J100").Rows.SpecialCells(xlCellTypeVisible), Range("L13:L100").Rows.SpecialCells(xlCellTypeVisible)))