简体   繁体   中英

How to automatically hide rows in excel

I have an attendance tracker that I use to send out an end of shift report. Currently, my entire class's attendance show. I'd like to make a macro that hides rows with 0 time missed.

Blank Tracker 2.0 is the name of the book Pulse Template is the name of the worksheet I need the macro for F is the column in which I record time missed (drawn from another sheet) 18-46 are the rows that need to be either hidden or unhidden.

So if F18 is 0, I need it to be hidden. If F18 > 0, I need it visible.

I figure this is a fairly simply macro but I have essentially no experience with macros whatsoever.

An efficient way is to use Union to gather all the rows to hide and then hide all in one go. Also, this code checks the values are actually numeric in cells.

You can tie this to a form control or ActiveX button as mentioned in prior answer.

Sub hideRows()


Dim wb As Workbook
Dim wsSource As Worksheet

Set wb = ThisWorkbook
Set wsSource = wb.Worksheets("Sheet7")

Dim loopRange As Range
Dim currRow As Range
Dim hideRange As Range

Set loopRange = wsSource.Range("F18:F46")
loopRange.EntireRow.Hidden = False

For Each currRow In loopRange.Rows

    If IsNumeric(currRow.Value2) And currRow.Value2 = 0 Then

        If Not hideRange Is Nothing Then
            Set hideRange = Union(currRow, hideRange)
        Else
           Set hideRange = currRow
        End If

    End If

Next currRow

If Not hideRange Is Nothing Then hideRange.EntireRow.Hidden = True


End Sub

How about this...

Sub HideRowsLoop()
    For Each cell In Worksheets("Pulse Template").Range("C18:C46")
        If cell.Value = "0" Then
            cell.EntireRow.Hidden = True
        End If
    Next cell
End Sub

Put this on a activeX button event. If F18 to F46 is 0, it hides, if not, it shows.

Sub button_click()
    Dim i as integer
    For i = 18 to 46
        If Activesheet.Range("F" & i) = 0 Then
            Activesheet.Range("F" & i).entireRow.Hidden = True
        else
            Activesheet.Range("F" & i).entireRow.Hidden = False
        End If
    Next i
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM