gpt4 book ai didi

VBA Excel For Loop 未按预期运行

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

所以我有两个 Subs,一个叫 Show_Hide,一个叫 hideAllRows。这两个子项都用于折叠 excel 工作表中的大量行,我有一个 for 循环,用于检查包含内容的单元格部分,并告诉代码不要折叠这些单元格。 for 循环嵌入在不工作的 hideAllRows 子中。该代码不会返回任何错误,它只是不起作用,因为在执行 hideAllRows 时隐藏了具有内容的单元格。

ShowHide 子在按下按钮时在 hideAllRows 子中被调用,这允许我使用 hideAllRows 子来引用我想要显示或隐藏的行,这部分有效。我正在使用 For 循环来检测行是否有内容,如果有,工作表不应该隐藏它们,而是工作表应该显示这些行并隐藏不包含内容的单元格。关于我在这里做错了什么有什么建议吗?下面的代码

 Sub Show_Hide(RowColumn As String, RangeSelected As String, Hide As Boolean)
Application.Calculation = xlCalculationManual
If RowColumn = "Column" Then
Columns(RangeSelected).Select
Selection.EntireColumn.Hidden = Hide
Else
Rows(RangeSelected).Select
Selection.EntireRow.Hidden = Hide
End If
Application.Calculation = xlCalculationAutomatic
End Sub



Sub hideAllRows()

Call Show_Hide("Row", "7:57", True)
Call Show_Hide("Row", "59:68", True)
Call Show_Hide("Row", "70:169", True)
Call Show_Hide("Row", "171:180", True)
Call Show_Hide("Row", "182:281", True)
Call Show_Hide("Row", "283:332", True)
Call Show_Hide("Row", "334:383", True)
Call Show_Hide("Row", "385:434", True)
Call Show_Hide("Row", "436:485", True)
Call Show_Hide("Row", "487:496", True)
Call Show_Hide("Row", "498:507", True)
Call Show_Hide("Row", "509:518", True)

Call Show_Hide("Row", "529:578", True)
Call Show_Hide("Row", "580:589", True)
Call Show_Hide("Row", "591:701", True)
Call Show_Hide("Row", "703:802", True)
Call Show_Hide("Row", "804:853", True)
Call Show_Hide("Row", "855:904", True)
Call Show_Hide("Row", "906:955", True)
Call Show_Hide("Row", "957:1006", True)
Call Show_Hide("Row", "1008:1017", True)
Call Show_Hide("Row", "1019:1028", True)
Call Show_Hide("Row", "1030:1039", True)
Call Show_Hide("Row", "1041:1090", True)
Call Show_Hide("Row", "1092:1141", True)
Call Show_Hide("Row", "1143:1192", True)
Call Show_Hide("Row", "1194:1244", True)
Call Show_Hide("Row", "1261:1268", True)



For i = 7 To 1269
If i = 7 Then i = 1269
Stuff = ActiveSheet.Cells(i, 1).Value
If Stuff <> "" Then
Rows(i & ":" & i).Select
Selection.EntireRow.Hidden = False
End If
Next i
Call UpdateButton(ActiveSheet.Shapes("Button13" & ActiveSheet.Name), "Show All Rows", "showAllRows")
End Sub

最佳答案

在这里,在 If i = 7 Then i = 1269'你在强制 For循环退出。

您需要重新检查该部分。

关于VBA Excel For Loop 未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25479587/

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