+ Reply to Thread
Results 1 to 3 of 3

VBA Macro to hide rows depending on the cell value - Can I do this multiple times?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2013
    Location
    Staffordshire
    MS-Off Ver
    Excel 2010
    Posts
    2

    Unhappy VBA Macro to hide rows depending on the cell value - Can I do this multiple times?

    Hello,

    I'm very new to VBA and could really do with some help, I've got the following code to work:

    Sub test1()

    If Range("sheet2!c9") = 0 Then
    ActiveSheet.Rows("10:11").EntireRow.Hidden = True
    Else

    If Range("sheet2!c9") <> 0 Then
    ActiveSheet.Rows("10:11").EntireRow.Hidden = False
    Else


    End If: End If:


    End Sub



    But I need to repeat this depending on the value in other cells on the same page to hide other rows?

    I've read other posts but can't seem to get anything to work.

    Please help, Regards Natalie

  2. #2
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: VBA Macro to hide rows depending on the cell value - Can I do this multiple times?

    Firstly, you don't need that second IF statement. The target cell will either be a 0 value or not....therefore you just need the one IF - ELSE:

    
    If range("Sheet2!C9").value = 0 then
         rows("10:11").entirerow.hidden = true
    else                                                               '  ie, cell C9 doesn't equal 0.
         rows("10:11").entirerow.hidden = false
    end if
    to add other cells / rows to this, simply copy the code you're using and change the cell / row reference. eg:

    
    If range("Sheet2!C9").value = 0 then
         rows("10:11").entirerow.hidden = true
    else
         rows("10:11").entirerow.hidden = false
    end if
    
    if range("Sheet2!G26").value = 0 then
         rows("30:31").entirerow.hidden = true
    else
         rows("30:31").entirerow.hidden = false
    end if

  3. #3
    Registered User
    Join Date
    08-21-2013
    Location
    Staffordshire
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA Macro to hide rows depending on the cell value - Can I do this multiple times?

    Thanks Swoop99, I've used:


    Sub Worksheet_Change(ByVal Target As Range)

    'Set 1
    If Range("N8").Value = 0 Then
    ActiveSheet.Rows("12:14").EntireRow.Hidden = True
    Else
    ActiveSheet.Rows("12:14").EntireRow.Hidden = False
    End If

    'Set 2
    If Range("N18").Value = 0 Then
    ActiveSheet.Rows("21:23").EntireRow.Hidden = True
    Else
    ActiveSheet.Rows("21:23").EntireRow.Hidden = False
    End If

    'Set 3
    If Range("N29").Value = 0 Then
    ActiveSheet.Rows("30:33").EntireRow.Hidden = True
    Else
    ActiveSheet.Rows("30:33").EntireRow.Hidden = False
    End If

    'Set 4
    If Range("N35").Value = 0 Then
    ActiveSheet.Rows("39:41").EntireRow.Hidden = True
    Else
    ActiveSheet.Rows("39:41").EntireRow.Hidden = False
    End If

    End Sub



    Thanks for your reply!

+ 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. copy a cell multiple times depending on cell value starting on a specific cell
    By weritadiojomiel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-17-2013, 05:25 AM
  2. Macro to Hide/Unhide Multiple Rows Based on Cell Value
    By mbp727 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2013, 11:12 AM
  3. [SOLVED] Macro to read cell and hide all rows where the text occurs less than 10 times in the sheet
    By CEDarnell in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2012, 12:59 PM
  4. Macro to hide colums depending on another cell value
    By Badvgood in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-25-2009, 08:08 AM
  5. Macro to hide rows and columns depending on the values of different ranges
    By echuser1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-13-2007, 12:47 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