1

请建议在 Excel 2003 中迭代 800 行以隐藏和取消隐藏行的解决方案。

For i = 0 To WSTreatmentOutComes.Range("F14:F813").Rows.Count - 1

                If WSTreatmentOutComes.Range("F" & 14 + i).Value = "" Or WSTreatmentOutComes.Range("F" & 14 + i).Value = "0" Then
                    For j = 0 To 9
                        WSTreatmentOutComes.Range("F" & 14 + i + j).Rows.Hidden = True
                    Next j
                Else
                    For k = 0 To 9
                        If WSTreatmentOutComes.Range("G" & 14 + i + k).Value = "" Or WSTreatmentOutComes.Range("G" & 14 + i + k).Value = "0" Then
                        WSTreatmentOutComes.Range("F" & 14 + i + k).Rows.Hidden = True
                        End If
                    Next k
                End If
                i = i + 9
            Next i
4

4 回答 4

4

在 A 列中进行如下公式测试:

Range("A14:A813").formula="=IF(AND(F:F="""",G:G=0),NA(),"""")"

然后使用这样的代码来隐藏或取消隐藏:

' first, unhide all rows:

WSTreatmentOutComes.rows.hidden=false
'
'now hide the ones where there is an NA# error from the formula:
WSTreatmentOutComes.Range("A14:A813").SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Hidden = True

最后,清理 A 列:

Range("A14:A813").clearcontents

你去那里,立即隐藏它们

StackOverFlow上有关此方法/技术的更多信息:how-to-delete-multiple-rows-without-a-loop-in-excel-vba

于 2013-04-11T13:26:38.727 回答
1

看来您应该能够通过一次性隐藏所有块来摆脱内部循环。接近这个的东西应该可以工作:

WSTreatmentOutComes.Range("F" & 14 + i & ":F" & 14 + i + 9).Rows.Hidden = True

另外,没有看到代码的开头,您是否在开始时关闭 ScreenUpdating?

Application.ScreenUpdating = False
<your block of code here>
Application.ScreenUpdating = True
于 2013-04-11T13:11:07.187 回答
1

我会这样做:

Option Explicit

Public Sub MacroHider()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim i As Long, j As Long, k As Long
    Dim rng As Range, vals() As Variant
    Set rng = WSTreatmentOutComes.Range("F14:F813")
    'get all of the data in the range'
    ReDim vals(1 To 800, 1 To 1)
    vals = rng

    For i = 1 To UBound(vals, 1) Step 10

        If vals(i) = "" Or vals(i) = "0" Then
            For j = 0 To 9
                rng.Rows(i + j).Hidden = True
            Next j
        Else
            For k = 1 To 9  '(already checked the first row)'
                If vals(i + k) = "" Or vals(i + k) = "0" Then
                    rng.Rows(i + k).Hidden = True
                End If
            Next k
        End If

    Next i

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub
于 2013-04-11T14:15:11.393 回答
0

使用此代码,我隐藏 A 列为“1”的行:

Sub Test()
    For i = 1 To Sheet1.Range("A1:A50").Rows.Count    
        If Sheet1.Range("A" & i).Value = "1" Then
            Sheet1.Range("A" & i).Rows.Hidden = True
        End If
    Next
End Sub

因此,如果您想在满足该行中的某个条件时隐藏行,您只需调整 If 条件和范围。

于 2013-04-11T13:01:35.263 回答