Excel VBA - 隐藏其他行时隐藏行

时间:2014-07-23 13:23:54

标签: excel excel-vba vba

对不起,我已经搜索过,但只找到了与我相似的解决方案,我觉得应该可行!我在财务工作表中设置了一个按钮,用于隐藏四个列中每个列中值为0的行。这工作正常,除非我有一个分隔行,如果隐藏小计行本身我也想隐藏它。它隐藏了我遇到问题的那一行:

Private Sub CommandButton1_Click()
'macro hides rows if all four columns contain zero values

'declare and initialize variables
Dim Col1 As String        'stores the column letter for the first column to examine
    Col1 = "C"
Dim Col2 As String        'stores the column letter for the second column to examine
    Col2 = "D"
Dim Col3 As String        'stores the column letter for the third column to examine
    Col3 = "E"
Dim Col4 As String        'stores the column letter for the fourth column to examine
    Col4 = "F"
Dim ListBottom As String  'stores the cell reference of the column that is populated for each record
    ListBottom = "A65536"
Dim FirstRow As Long      'first row with data to inspect
    FirstRow = 3

'declare and initialize system variables
Dim LastRow As Long       'store the last row with data
    LastRow = 300 'Range(ListBottom).End(xlUp).Row   'moves up to the last row with data

Application.ScreenUpdating = False

For x = FirstRow To LastRow
    If Cells(x, Col1).Value = "0" And Cells(x, Col2).Value = "0" And Cells(x, Col3).Value = "0" And Cells(x, Col4).Value = "0" Then
       Cells(x, Col1).EntireRow.Hidden = True

        'Expenses section tenant, utilites and maint - hides spacing and underlines if data rows are hidden'
        If Cells("C73").Value = 0 And Cells("D73").Value = 0 And Cells("E73").Value = 0 And Cells("F73").Value = 0 Then
            Rows("72").EntireRow.Hidden = True
        End If
        If Rows("82").EntireRow.Hidden = True Then
            Rows("81").EntireRow.Hidden = True And Rows("83").EntireRow.Hidden = True
        End If
     End If
Next x
Application.ScreenUpdating = True
End Sub

隐藏0行的顶部部分工作正常,它在底部我无法工作。我已经尝试了两种不同的方式(从第29行开始),我尝试过但都没有。

如果有更优雅的方式来做到这一点,我当然愿意接受它。非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

这个怎么样?

Private Sub CommandButton1_Click()
'macro hides rows if all four columns contain zero values

'declare and initialize variables
Dim Col1 As String        'stores the column letter for the first column to examine
    Col1 = "C"
Dim Col2 As String        'stores the column letter for the second column to examine
    Col2 = "D"
Dim Col3 As String        'stores the column letter for the third column to examine
    Col3 = "E"
Dim Col4 As String        'stores the column letter for the fourth column to examine
    Col4 = "F"
Dim ListBottom As String  'stores the cell reference of the column that is populated for each record
    ListBottom = "A65536"
Dim FirstRow As Long      'first row with data to inspect
    FirstRow = 3

'declare and initialize system variables
Dim LastRow As Long       'store the last row with data
    LastRow = 300 'Range(ListBottom).End(xlUp).Row   'moves up to the last row with data

Application.ScreenUpdating = False

For x = FirstRow To LastRow
    If Cells(x, Col1).Value = "0" And Cells(x, Col2).Value = "0" And Cells(x, Col3).Value = "0" And Cells(x, Col4).Value = "0" Then
       Cells(x, Col1).EntireRow.Hidden = True

       'Expenses section tenant, utilites and maint - hides spacing and underlines if data rows are hidden'
       Cells(x+1, Col1).EntireRow.Hidden = True
     End If
Next x
Application.ScreenUpdating = True
End Sub

不是验证行是否隐藏,而是在隐藏主行的同时隐藏辅助行。

答案 1 :(得分:0)

不要惊慌,解决方案应该非常简单(我会尝试为你的代码提供一些“优雅”):D

Private Sub CommandButton1_Click()
' You don't need to Dim all those variables. Also they were constants not variables :)
' In case you need to declare a constant use "Const x As yourType = value"

Dim ListBottom As String  'stores the cell reference of the column that is populated for each record
Dim FirstRow As Long      'first row with data to inspect
Dim LastRow As Long       'store the last row with data

ListBottom = "A65536" ' You can address the last cell with some commands like .SpecialCells or .End. I strongly suggest googling them up ;)
FirstRow = 3
LastRow = 300  'moves up to the last row with data

Application.ScreenUpdating = False

For x = FirstRow To LastRow
   If Cells(x, 3).Value = "0" And Cells(x, 4).Value = "0" And Cells(x, 5).Value = "0" _ 
     And Cells(x, 6).Value = "0" Then ' WARNING: "0" indicates a STRING in which 
     ' you find a Chr 0. When you (like below) say = 0 without the "", it means 
     ' that you're comparing to a NUMERIC value. In this case, it would do good 
     ' storing your values in some variables with a numeric type (Integer, Long, 
     ' Double) or a string and then compare them as fail-safe!

         Cells(x, 3).EntireRow.Hidden = True
        'Expenses section tenant, utilites and maint - hides spacing and underlines if data rows are hidden'
        If Cells("C73").Value = 0 And Cells("D73").Value = 0 And Cells("E73").Value = 0 And Cells("F73").Value = 0 Then
            Rows("72").EntireRow.Hidden = True
        End If
        If Rows("82").EntireRow.Hidden = True Then
            Rows("81").EntireRow.Hidden = True ' DANGER: for multiple commands you
            ' can't simply give "command" AND "command. All things that are between
            ' "Then" and "End If" are executed sequentially
            Rows("83").EntireRow.Hidden = True
        End If
    End If
Next x

Application.ScreenUpdating = True

End Sub

没有测试它但应该工作。我建议用我在代码中说过的那些东西做一些测试,它们可以在将来帮助你很多!