+ Reply to Thread
Results 1 to 5 of 5

Hiding rows based on a certain cell's value which is a formula

Hybrid View

  1. #1
    Registered User
    Join Date
    07-28-2015
    Location
    Calhoun, Louisiana
    MS-Off Ver
    Microsoft 2013
    Posts
    3

    Hiding rows based on a certain cell's value which is a formula

    It's been years since I have did anything with VBA and macros and cannot figure out what I am doing wrong. I have a workbook that has multiple worksheets including a Cover Sheet, Template Sheet, and Location Sheet, as well as others. I have created specific templates on the template sheet and location sheet which pull data from the cover sheet. I have programmed the template sheet so that the sheets are blank until I generate data on the cover sheet from a data sheet at which point it populates the data in the template sheet. When I populate data and print the template sheet, it prints all 33 sheets even though there may only be 1 or 2 pages that actually display values. I have formulas that return data on the template sheet in columns A-F. Column B is my reference cell column that I am trying to use to hide or unhide the rest of the rows. The code that I attempted to create and use is below. I want it to look at cell B21 and if B21 is blank as derived from a formula ='Cover Sheet"!$A$9 due to A9 on the Cover Sheet being blank, I want rows 1-34 to be hidden and so on for the other rows. I have tried if then statements in my macros but no luck either. I know that I can use print selection to achieve what I am after but I am going to create a print button to make it easier for other users of this spreadsheet.

    Private Sub Worksheet_Calculate()
     Dim MyResult As String
    
     Application.EnableEvents = False
    
     MyResult = Worksheets("Template Sheet").Cells(21, 2).Value
    
     Select Case MyResult
    
     Case ""
     Rows("1:34").EntireRow.Hidden = True
    
     End Select
    
     MyResult2 = Worksheets("Template Sheet").Cells(55, 2).Value
    
     Select Case MyResult2
    
     Case ""
     Rows("35:68").EntireRow.Hidden = True
    
     End Select
    
     MyResult3 = Worksheets("Template Sheet").Cells(89, 2).Value
    
     Select Case MyResult3
    
     Case ""
     Rows("69:102").EntireRow.Hidden = True
    
     End Select
     
     MyResult4 = Worksheets("Template Sheet").Cells(123, 2).Value
    
     Select Case MyResult4
    
     Case ""
     Rows("103:136").EntireRow.Hidden = True
    
     End Select
     
     MyResult5 = Worksheets("Template Sheet").Cells(157, 2).Value
    
     Select Case MyResult5
    
     Case ""
     Rows("137:170").EntireRow.Hidden = True
    
     End Select
     
     Application.EnableEvents = True
    
     End Sub
    Last edited by jbh040; 07-30-2015 at 04:51 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Hiding rows based on a certain cell's value which is a formula

    Try this instead:

    Private Sub Worksheet_Calculate() 
    
     Application.EnableEvents = False
     
     Rows.Hidden = False
    
     If Worksheets("Template Sheet").Cells(21, 2).value = "" Then _
     Rows("1:34").Hidden = True
     
     If Worksheets("Template Sheet").Cells(55, 2).value = "" Then _
     Rows("35:68").Hidden = True
     
     If Worksheets("Template Sheet").Cells(89, 2).value = "" Then _
     Rows("69:102").Hidden = True
     
     If Worksheets("Template Sheet").Cells(123, 2).value = "" Then _
     Rows("103:136").Hidden = True
     
     If Worksheets("Template Sheet").Cells(157, 2).value = "" Then _
     Rows("137:170").Hidden = True
     
     Application.EnableEvents = True
    
     End Sub
    Last edited by xladept; 07-30-2015 at 07:11 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    07-28-2015
    Location
    Calhoun, Louisiana
    MS-Off Ver
    Microsoft 2013
    Posts
    3

    Re: Hiding rows based on a certain cell's value which is a formula

    I figured out the issue. Because I had the sheet protected, it was not allowing the macro to run. Can anyone tell me the code to have that macro unprotect the sheet and then run the macro and then protect the sheet again.

  4. #4
    Registered User
    Join Date
    07-28-2015
    Location
    Calhoun, Louisiana
    MS-Off Ver
    Microsoft 2013
    Posts
    3

    Re: Hiding rows based on a certain cell's value which is a formula

    Thank you for the reply. I used this code but was unsuccessful. I am getting the RunTime Error '1004': Unable to set the Hidden property of the range class. I had tried the if then statements before and got this error.

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Hiding rows based on a certain cell's value which is a formula

    Maybe:

    Private Sub Worksheet_Calculate()  
    ActiveSheet.Unprotect ("Password")
     Application.EnableEvents = False
     
     Rows.Hidden = False
    
     If Worksheets("Template Sheet").Cells(21, 2).value = "" Then _
     Rows("1:34").Hidden = True
     
     If Worksheets("Template Sheet").Cells(55, 2).value = "" Then _
     Rows("35:68").Hidden = True
     
     If Worksheets("Template Sheet").Cells(89, 2).value = "" Then _
     Rows("69:102").Hidden = True
     
     If Worksheets("Template Sheet").Cells(123, 2).value = "" Then _
     Rows("103:136").Hidden = True
     
     If Worksheets("Template Sheet").Cells(157, 2).value = "" Then _
     Rows("137:170").Hidden = True
     ActiveSheet.Protect ("Password")
     Application.EnableEvents = True
    
     End Sub
    *If there is no password then just delete that part.

    And - Thanks for the rep!

+ 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. Hiding Rows based on a cell Value that is a result of a formula
    By erydius in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2014, 08:30 PM
  2. [SOLVED] (SOLVED) Hiding rows based on a cell value that comes from a formula
    By Dbouwheer in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-26-2014, 08:15 AM
  3. [SOLVED] Hiding rows based on cell value.
    By Metalgijs in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-12-2013, 03:08 AM
  4. Hiding rows in one tab based on cell value in another
    By hoffmt28 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2013, 09:27 AM
  5. Hiding Rows based on Formula result
    By Viscount_Grey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-21-2011, 08:59 AM
  6. Hiding rows based on value in another cell
    By sahar in forum Excel General
    Replies: 0
    Last Post: 10-12-2011, 10:27 AM
  7. Hiding Rows Based based upon cell value being blank
    By leelee23 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2007, 04:48 AM

Tags for this Thread

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