+ Reply to Thread
Results 1 to 16 of 16

Macro that will delete/hide rows based on a cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    03-23-2013
    Location
    lincoln england
    MS-Off Ver
    2010
    Posts
    75

    Macro that will delete/hide rows based on a cell value

    Hi

    To keep this really simple I need a macro that will do the following.

    If Sheet1!A1 =1 then hide the following rows Sheet2!123.

    Thanks in advance.
    Dan

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that will delete/hide rows based on a cell value

    Nothing is simple in case of VBA..

    try this..

    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Application.DisplayAlerts = False
        
        If Target.Address = "$A$1" Then
            On Error GoTo Deb
                Sheets("Sheet2").Range("1:3").EntireRow.Hidden = Target.Value = 1
        End If
    Deb:
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    End Sub
    Need to place in Sheet Module.

    PS: Slightly Changed
    Last edited by Debraj Roy; 11-24-2014 at 12:33 PM.
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro that will delete/hide rows based on a cell value

    Hello dwilkinson12,

    You may try;

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Range("A1").Value = 1 Then
    Sheet2.Range("A123").Rows.EntireRow.Hidden = True
    End If
    
    End Sub
    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that will delete/hide rows based on a cell value

    Maybe:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        If Target.Value = 1 Then
            Sheets("Sheet2").Rows("1:3").Hidden = True
        Else
            Sheets("Sheet2").Rows("1:3").Hidden = False
        End If
    End If
    End Sub

  5. #5
    Registered User
    Join Date
    03-23-2013
    Location
    lincoln england
    MS-Off Ver
    2010
    Posts
    75

    Re: Macro that will delete/hide rows based on a cell value

    Everytime I run all 3 of these it wants me to name the macro and that isn't working as it just creates a blank piece of vb code.

    Dan

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Macro that will delete/hide rows based on a cell value

    Dan, are you putting the code in a standard module? If yes, then that is your mistake. These are sheet-level code that are triggered by events in the sheet. The code goes in the sheet code, not a standard module and you don't run it in the vba window. You run it by making a change in the worksheet.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  7. #7
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that will delete/hide rows based on a cell value

    Check the attached..

    EF1051546.xlsm

  8. #8
    Registered User
    Join Date
    03-23-2013
    Location
    lincoln england
    MS-Off Ver
    2010
    Posts
    75

    Re: Macro that will delete/hide rows based on a cell value

    Hi I downloaded the attachment and ran the macro but it's doing the same. Everytime I click play/run in VB Editor it just pops up with a new macro box.

    Thanks,
    Dan

  9. #9
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that will delete/hide rows based on a cell value

    Did you tried.. by changing cell "A1" ..

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Macro that will delete/hide rows based on a cell value

    Tested this code and works fine:

    Put in Sheet1's code:
    Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Address = "$A$1" Then Sheets("Sheet2").Range("1:3").EntireRow.Hidden = Target.Value = 1
    End Sub

  11. #11
    Registered User
    Join Date
    03-23-2013
    Location
    lincoln england
    MS-Off Ver
    2010
    Posts
    75

    Re: Macro that will delete/hide rows based on a cell value

    When I paste into vb or even just open up the document that was attached above. It is within the sheet tab. I click on run macro and it pops up with the same box that would appear if I was trying to assign a macro to a button. I don't think excel is picking up on the fact there is a macro there. I think it has something to do with the name as the macro doesn't appear in my macros list even if i change it from Private Sub to just Sub? Anymore suggestions?

    Macros are definitely enabled.

    Cheers,
    Dan

  12. #12
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that will delete/hide rows based on a cell value

    Hi Dan,

    This macro is a "Event Driven" code.. you dont have to pres / click anything to get it fired.
    Simply change the value in Sheet 1.. and it will reflect its work..

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro that will delete/hide rows based on a cell value

    Hello dwilkinson12,

    I think we are now at a stage where we would want to see what your Workbook looks like.

    Please attached a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  14. #14
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that will delete/hide rows based on a cell value

    Dan:

    Look here:

    http://www.contextures.com/xlvba01.html

    Refer to the section Copy Excel VBA Code to a Worksheet Module. All of the codes suggested above should work for you.

  15. #15
    Registered User
    Join Date
    03-23-2013
    Location
    lincoln england
    MS-Off Ver
    2010
    Posts
    75

    Re: Macro that will delete/hide rows based on a cell value

    Sorry about this guys I was being stupid, Debraj Roy thankyou didn't realise that the macro didn't have to be manually ran.

    Thanks,
    Dan

  16. #16
    Registered User
    Join Date
    03-23-2013
    Location
    lincoln england
    MS-Off Ver
    2010
    Posts
    75

    Re: Macro that will delete/hide rows based on a cell value

    I have incorporated this into my master sheet and included this code in a bigger macro.

    Yesterday I was able to run a copy and paste special (values) macro to update the reference cell and then your macro would work and hide various rows.

    However today your macro will not update or hide the cells unless I go to that reference cell manually and change the number. Any ideas?

    Cheers,
    Dan

+ 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. [SOLVED] Macro to Show/Hide Rows Based on Text in Cell
    By HCopeland3 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-01-2014, 03:56 PM
  2. [SOLVED] Macro to automatically hide or unhide rows based on either a value or no value in a cell
    By WFP111 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-04-2013, 12:28 PM
  3. Hide / Delete rows based on value in cell
    By lgimnich in forum Excel General
    Replies: 2
    Last Post: 06-30-2012, 09:52 AM
  4. Macro to hide rows based on cell
    By aeewing in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2011, 10:24 AM
  5. [SOLVED] Hide/Delete entire rows based in the content of one cell
    By Clueless in forum Excel General
    Replies: 2
    Last Post: 10-03-2005, 10:05 AM

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