VBA Script

logansauter6

New Member
Joined
Aug 27, 2014
Messages
7
I am needing a VBA formula or code that will hide entire rows based on a value in the first cell in each row. I have a workbook and want to have the rows that are blank to be hidden if there are no selections present in the row.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Code:
Sub hideRows()

    Dim rowStart As Integer
    Dim rowEnd As Long
    
    rowStart = 2
    rowEnd = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    
    For x = rowStart To rowEnd
        If Cells(x, 1) = "" Then
            Rows(x).Hidden = True
        End If
    Next x


End Sub

This assumes column B is populated and uses column A as the trigger to hide rows. If column A is blank, hide the row.
 
Upvote 0
IF Column A contains data only (no formulas), then you can use this non-looping macro as well...
Code:
Sub HideRowsIfBlankCellInColumnA()
  Range("A1:A" & Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row).SpecialCells(xlBlanks).EntireRow.Hidden = True
End Sub
 
Upvote 0
Thank you both. How do I make the macro work? I guess I am not as excel savvy as I thought. I put it in and tried to run the macro but nothing happened.
 
Upvote 0
Thank you both. How do I make the macro work? I guess I am not as excel savvy as I thought. I put it in and tried to run the macro but nothing happened.

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top