gpt4 book ai didi

excel - 给定多个范围,如果满足多个条件,则隐藏行

转载 作者:行者123 更新时间:2023-12-04 21:48:38 25 4
gpt4 key购买 nike

我试图在这种情况下隐藏行:

#given some ranges within column B and C 
#for each row within the ranges
#if the related cells in column B and C both have value equal to 0
#then hide the specific row

这是我正在使用的代码,但我正在努力实现两个单元格必须具有 value = 0 的 if 部分
Sub HideRows()

Dim r0 As Range, r1 As Range, r2 As Range, MultiRange As Range, r As Range

Set r0 = Sheets("Sheet1").Range("B1:C6")
Set r1 = Sheets("Sheet1").Range("B8:C8")
Set r2 = Sheets("Sheet1").Range("B10:C11")
Set MultiRange = Union(r0, r1, r2)

For Each r In MultiRange
r.EntireRow.Hidden = (r.Value = 0)
Next r

End Sub

最佳答案

你很亲密。事实上,您已经解决了较难的部分(联合范围)。将循环设置为通过 rows在联合范围内,而不是默认的 cells

Sub HideRows()

Dim r0 As Range, r1 As Range, r2 As Range, MultiRange As Range, r As Range

Set r0 = Sheets("Sheet1").Range("B1:C6")
Set r1 = Sheets("Sheet1").Range("B8:C8")
Set r2 = Sheets("Sheet1").Range("B10:C11")
Set MultiRange = Union(r0, r1, r2)

For Each r In MultiRange.Rows
r.EntireRow.Hidden = (r.Cells(1,1).value = 0 AND r.cells(1, 2).value = 0)
Next r

End Sub

关于excel - 给定多个范围,如果满足多个条件,则隐藏行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58999685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com