提问人:Ivan Huang 提问时间:10/19/2023 最后编辑:Ivan Huang 更新时间:10/19/2023 访问量:54
对于具有两个条件的语句
For Statement with Two Conditions
问:
我试着把所有的代码组合在一起,让它一次只看一条记录。代码的第二部分是查看循环中的所有行。理想情况下,我想查看客户 A 的记录并隐藏行 #66 或行 #64。之后,代码应该从顶部重新开始查看客户 B、C、D...感谢回答我关于 for loop 问题的人。谢谢
Dim c As Range
Dim d As Range
With ThisWorkbook.Sheets("Template")
.Rows.EntireRow.Hidden = False
.Activate
For Each c In .Range("E27,E32,E35,L36,L37,E39,E40,E41,E42,E43,E44,E45,E46,E49,E50,E51")
If c.Value = 0 Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next
End With
With Worksheets("Template")
If .Range("E26").Value = 0 Or .Range("E27") = 0 Then
Worksheets("Template").Rows("69").EntireRow.Hidden = True
Else
Worksheets("Template").Rows("69").EntireRow.Hidden = False
End If
If .Range("E32").Value = 0 Then
Worksheets("Template").Rows("70").EntireRow.Hidden = True
Else
Worksheets("Template").Rows("70").EntireRow.Hidden = False
End If
End With
Dim r As Range
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets("Data File")
For Each r In ws2.Range("CU3:CU5")
If r.Value = "RSU" And r.Offset(0, 1).Value = "Y" Then
Worksheets("Template").Rows("66").EntireRow.Hidden = False
Worksheets("Template").Rows("64").EntireRow.Hidden = True
Else
Worksheets("Template").Rows("66").EntireRow.Hidden = True
Worksheets("Template").Rows("64").EntireRow.Hidden = False
End If
Next r
答:
1赞
Tim Williams
10/19/2023
#1
这是您可以做到这一点的一种方法,但我仍然对如何解释您正在循环的范围内的多行感到困惑 - 您将从该范围的最后一行开始进行匹配。
Dim rw As Range, matched As Boolean
For Each rw In ws2.Range("CU3:CV5").Rows 'loop over rows in range
'row matches conditions?
matched = rw.Cells(1).Value = "RSU" And rw.Cells(2).Value = "Y"
With Worksheets("Template")
.Rows("66").EntireRow.Hidden = Not matched
.Rows("64").EntireRow.Hidden = matched
End With
Next rw
编辑:更新后
Dim c As Range, ws As Range
Set ws = ThisWorkbook.Worksheets("Template")
ws.Rows.EntireRow.Hidden = False
For Each c In ws.Range("E27,E32,E35,L36,L37,E39,E40,E41,E42,E43,E44,E45,E46,E49,E50,E51").Cells
If c.Value = 0 Then c.EntireRow.Hidden = c.Value = 0
Next
ws.Rows(69).EntireRow.Hidden = ws.Range("E26").Value = 0 Or ws.Range("E27") = 0
ws.Rows(70).EntireRow.Hidden = ws.Range("E32").Value = 0
评论
0赞
Ivan Huang
10/19/2023
我发布了我所有的代码。我有一个新的挑战,因为 for 循环将查看循环中的所有记录,而不是一次查看一行。我希望代码在隐藏第 64 行或第 66 行后从头开始
0赞
taller
10/19/2023
#2
这就是你要找的。这和@Tim的评论是一样的。您不必保留 in 子句。Two Conditions
For
Dim r As Range
For Each r In ws2.Range("CU3:CU5")
If r.Value = "RSU" And r.Offset(0, 1).Value = "Y" Then
Worksheets("Template").Rows("66").EntireRow.Hidden = False
Worksheets("Template").Rows("64").EntireRow.Hidden = True
Else
Worksheets("Template").Rows("66").EntireRow.Hidden = True
Worksheets("Template").Rows("64").EntireRow.Hidden = False
End If
Next r
代码可以简化如下。
Dim r As Range, bFlag As Boolean
For Each r In ws2.Range("CU3:CU5")
bFlag = (r.Value = "RSU" And r.Offset(0, 1).Value = "Y")
With Worksheets("Template")
.Rows(66).EntireRow.Hidden = Not bFlag
.Rows(64).EntireRow.Hidden = bFlag
End With
Next r
评论
0赞
Ivan Huang
10/19/2023
非常感谢您的反馈。我需要发布另一个问题来向您展示所有内容。我应该一次发布我的代码。
评论
If r.Value = "RSU" And r.Offset(0, 1).Value = "Y" Then