+ Reply to Thread
Results 1 to 14 of 14

Macro to hide rows based on defined range in a column

  1. #1
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Macro to hide rows based on defined range in a column

    Hi all,

    I've done various searches for this but I only seem to be able to find vba code for using values of an entire column.

    Essentially, I just need a macro that says "if column B48:60 is 0 or " ", hide rows 248-291.

    Also, if there's any way of reversing this to unhide all (because the data might be different each time it is used), then that would be good too.

    I don't mind if it runs it automatically or is assignable to a button, I've had limited experience creating very basic macros but this is beyond me.

    I hope I've explained this clear enough,and thank you in advance.

    Kind regards,

    Derry

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to hide rows based on defined range in a column

    I just need a macro that says "if column B48:60 is 0 or " ", hide rows 248-291.
    Will this be static always?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Macro to hide rows based on defined range in a column

    Hi,

    Yes, those cells will always be static and relative to each other.

    I will need to expand on it at some point to reflect another range in another column, but I just need the basic macro and presume I can change the ranges as I need to?

    thanks

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to hide rows based on defined range in a column

    Should entire range of B48 to B60 be blank or have 0? What if there is data in some cells in that range?

  5. #5
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Macro to hide rows based on defined range in a column

    hi again,

    There may be data in some or all of those cells, it depends entirely on the source information the user has. I guess it could be easier specified as if B48 = 0 or " ", hide row 252, if B52=0 or " ", then hide row 253,B56 would hide row 254 etc down to B64- row 256

    The range in B would be every other cell from B48 to B60, and then rows are 253-256 consecutive. If it helps, the intervening cells in B (B49,B51 etc) are locked and won't have user entry, therefore if it is easier to specify that entire range then it won't cause any problems.

    thanks

  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Macro to hide rows based on defined range in a column

    Derry, this could easily be managed with worksheet event code

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to hide rows based on defined range in a column

    The range in B would be every other cell from B48 to B60
    I guess you mean every 3 rows. See the example you have given -
    I guess it could be easier specified as if B48 = 0 or " ", hide row 252, if B52=0 or " ", then hide row 253,B56 would hide row 254 etc down to B64- row 256
    Am i right?

  8. #8
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Macro to hide rows based on defined range in a column

    Based upon your latest info

    Please Login or Register  to view this content.
    Last edited by Bob Phillips; 03-27-2012 at 09:46 AM.

  9. #9
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Macro to hide rows based on defined range in a column

    Thanks Bob,

    I've just tried that code but doesn't appear to be working? Do I have to compile or anything before it will take effect?

    Arlette, with the cells, to give you a bit of background, The cells from the range in B autopopulate cells in the rows mentioned, ie, B48 populates in Row 258, B52 auto populates in row 259 and so on, that's why I need the rows hidden if there is nothing in the Column B range as the resultant output gets copied and otherwise has massive gaps in it unless the user deletes them all themselves.

    For example, B48 and B52 have data, but the rest of the range doesn't, it will then only show rows 258 and 259, and then hide the others.

    Thanks

  10. #10
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Macro to hide rows based on defined range in a column

    No but it is worksheet event code so it needs to be put in the relevant sheet code module (right-click the sheet tab and select View Code).

    To invoke it, enter or delete a value in B48:B60.

  11. #11
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Macro to hide rows based on defined range in a column

    Hi Bob,

    Sorry but I can't seem to get it to work, I have it in the section you mention but it just doesn't seem to be working

    The screen flickers momentarily like it is running a macro but doesn't hide the rows required.

    Any ideas?

    Thanks for your help with this.

  12. #12
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Macro to hide rows based on defined range in a column

    Can you post your workbook?

  13. #13
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Macro to hide rows based on defined range in a column

    Hi Bob,

    Unfortunately not without clearance from my boss, but by way of update, the macro works-sort of. Rather than than hiding the range specified, it is only hiding the rows relative to B48 and to B60, ie the first and last of the range, the ones relating to the middle always stay shown.

    Does this help?

  14. #14
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Macro to hide rows based on defined range in a column

    I'll take another look tomorrow, I am off out now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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