+ Reply to Thread
Results 1 to 8 of 8

Hide/unhide rows in excel using macro

Hybrid View

aaron061883 Hide/unhide rows in excel... 10-22-2013, 12:21 AM
:) Sixthsense :) Re: Hide/unhide rows in excel... 10-22-2013, 01:35 AM
HaHoBe Re: Hide/unhide rows in excel... 10-22-2013, 01:46 AM
AlvaroSiza Re: Hide/unhide rows in excel... 10-22-2013, 01:42 AM
AlvaroSiza Re: Hide/unhide rows in excel... 10-22-2013, 01:54 AM
aaron061883 Re: Hide/unhide rows in excel... 10-22-2013, 09:50 AM
HaHoBe Re: Hide/unhide rows in excel... 10-22-2013, 12:16 PM
aaron061883 Re: Hide/unhide rows in excel... 10-22-2013, 09:47 AM
  1. #1
    Forum Contributor
    Join Date
    09-11-2012
    Location
    minot, north dakota
    MS-Off Ver
    Excel 2010
    Posts
    127

    Hide/unhide rows in excel using macro

    I need help creating a macro that will hide and unhide rows when the macro button is clicked. I need to hide rows 11 through 28 if column "I" is blank. I would like to add the macro button to be able to hide the rows that do not have a value in column "I" but if rows need to be adjusted click the macro button to show all rows. If it is possible to make the macro to work for all sheets between the tabs labeled First and Last that would be a great bonus. Any help would be greatly appreciated.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Hide/unhide rows in excel using macro

    Try the below code…

    Sub HideRowsBasedOnCondtion()
    Dim iStart As Integer, iEnd As Long, i As Long, blHide As Variant
    
    iStart = Sheets("First").Index
    iEnd = Sheets("Last").Index
    
    blHide = MsgBox("Do You Want To Hide The Rows?", vbYesNo, "Hide Rows?")
    
    If blHide = vbNo Then GoTo UnhideRows
    
    For x = iStart To iEnd
        With Sheets(x)
            For i = 11 To 28
                If .Cells(i, "I").Value = "" Then
                    .Rows(i).Hidden = True
                Else
                    .Rows(i).Hidden = False
                End If
            Next i
        End With
    Next x
    
    MsgBox "Rows Hidden", vbInformation, "Task Completed"
    Exit Sub
    
    UnhideRows:
        For x = iStart To iEnd
            Sheets(x).Rows("11:28").Hidden = False
        Next x
        
        MsgBox "Rows Un-Hidden", vbInformation, "Task Completed"
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Hide/unhide rows in excel using macro

    Hi, aaron061883,

    based on the first code maybe
    Sub HideRowsBasedOnCondtion_2()
    Dim iStart As Integer, iEnd As Long, i As Long, blHide As Variant
    Dim x As Long
    
    iStart = Sheets("First").Index
    iEnd = Sheets("Last").Index
    
    blHide = MsgBox("Do You Want To Hide The Rows?", vbYesNo, "Hide Rows?")
    
    For x = iStart To iEnd
      With Sheets(x)
        .Rows("11:28").Hidden = False
        If blHide = vbYes Then
          For i = 11 To 28
            If .Cells(i, "I").Value = "" Then
              .Rows(i).Hidden = True
            End If
          Next i
        End If
      End With
    Next x
    
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Hide/unhide rows in excel using macro

    A for each approach with external calls via ActiveX Toggle Button.

    1. Copy and paste the following (all of it) into a standard module.
    Option Explicit
    Public wb As Workbook
    Public ws As Worksheet
    Public wsFirst As Worksheet
    Public wsLast As Worksheet
    Public rngCell As Range
    
    Sub AaronHide()
    
    Set wb = ThisWorkbook
    Set wsFirst = wb.Sheets("First")
    Set wsLast = wb.Sheets("Last")
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        For Each ws In wb.Sheets
            If ws.Name <> "First" And ws.Name <> "Last" Then
                For Each rngCell In ws.Range("I11:I28")
                    If rngCell.Value = "" Then
                        rngCell.EntireRow.Hidden = True
                    End If
                Next
            End If
        Next
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Goto Reference:=Worksheets("First").Range("A1"), scroll:=True
        End With
    
    End Sub
    
    Sub AaronShow()
    
    Set wb = ThisWorkbook
    Set wsFirst = wb.Sheets("First")
    Set wsLast = wb.Sheets("Last")
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        For Each ws In wb.Sheets
            If ws.Name <> "First" Or ws.Name <> "Last" Then
                ws.Range("I11:I28").EntireRow.Hidden = False
            End If
        Next
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Goto Reference:=Worksheets("First").Range("A1"), scroll:=True
        End With
    
    End Sub
    2. Create your button by drawing an ActiveX toggle button (Developer Tab > Controls Group > "Insert" > ToggleButton) onto the worksheet in your preferred location.

    3. Right-click the toggle button > View Code.

    4. Copy and Paste the following between the auto-created (by Excel) "Private Sub" and "End Sub":
    If ToggleButton1.Value = True Then
            Call AaronShow
            ToggleButton1.Caption = "Hide"
        Else
            Call AaronHide
            ToggleButton1.Caption = "Show"
        End If
    5. Deactivate "Design Mode" (also on the Developer Tab in the Controls Group)
    Last edited by AlvaroSiza; 10-22-2013 at 01:45 AM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  5. #5
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Hide/unhide rows in excel using macro

    Holger,

    Would your code still hold in a situation where the "First" and "Last" sheets are not actually the left-most and right-most worksheets in the workbook?

    #learning

  6. #6
    Forum Contributor
    Join Date
    09-11-2012
    Location
    minot, north dakota
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Hide/unhide rows in excel using macro

    Solved I am not sure how to add the solved feature to the post

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Hide/unhide rows in excel using macro

    Hi, AlvaroSiza,

    not a big thing to alter in the code:
    Sub HideRowsBasedOnCondtion_3()
    Dim iStart As Integer, iEnd As Long, i As Long, blHide As Variant
    Dim x As Long, lngStep As Long
    
    iStart = Sheets("First").Index
    iEnd = Sheets("Last").Index
    
    If iEnd < iStart Then
      lngStep = -1
    Else
      lngStep = 1
    End If
    
    blHide = MsgBox("Do You Want To Hide The Rows?", vbYesNo, "Hide Rows?")
    
    For x = iStart To iEnd Step lngStep
      With Sheets(x)
        .Rows("11:28").Hidden = False
        If blHide = vbYes Then
          For i = 11 To 28
            If .Cells(i, "I").Value = "" Then
              .Rows(i).Hidden = True
            End If
          Next i
        End If
      End With
    Next x
    
    End Sub
    @aaron061883:
    Quote Originally Posted by FAQ
    To mark your thread solved do the following:
    New Method
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved

    Old Method
    - Go to the first post
    - Click edit
    - Click Advance
    - Just below the word "Title:" you will see a dropdown with the word No prefix.
    - Change to Solve
    - Click Save
    Ciao,
    Holger

  8. #8
    Forum Contributor
    Join Date
    09-11-2012
    Location
    minot, north dakota
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Hide/unhide rows in excel using macro

    Yes that is the case as the first tab is actually the 6 tab in the workbook

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hide/Unhide Rows macro
    By nvalencia in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-08-2012, 01:44 PM
  2. Hide/Unhide rows using a macro
    By Jazzy Max in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-10-2012, 07:03 AM
  3. Hide/Unhide Rows Macro
    By wpwalsh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-07-2011, 08:42 AM
  4. Macro to hide/unhide rows using box
    By jgomez in forum Excel General
    Replies: 10
    Last Post: 03-05-2011, 03:41 AM
  5. Macro to Hide/Unhide Rows
    By pgwolfe in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-24-2008, 09:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1