Excel Macro: Run Macro Based on a cell value

Thanks Thanks:  0
Likes Likes:  0
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Excel Macro: Run Macro Based on a cell value

  1. #1
    New Member
    Join Date
    Jul 2017
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Excel Macro: Run Macro Based on a cell value

     
    Hi I'm trying to run a macro based on another cell value. I simply want to hide rows based on a dropdown value from another cell. Is this possible. I have working code that is hiding rows, but it is not being triggered when the value is selected I have to manually run the macro. Could someone help me?

    Code:

    Private Sub worksheet_change(ByVal target As Range)
    Set target = Range("B1")
    If target.value = "Delete" Then
    Call Macro1
    End If
    End Sub


    Sub Macro1

    If Range("B1").Value="Delete" Then
    Rows("3:4").EntireRow.Hidden = True .......etc


    The sub macro1 is working but not being triggered by the private sub on the worksheet

  2. #2
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    41,527
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    Welcome to the Board!

    In a Worksheet_Change procedure, you don't set the Target range. The Target range is the range that was updated that triggered the procedure to run.

    So I think you can do all that you want like this:

    Code:
    Private Sub worksheet_change(ByVal target As Range)
        If Target.Address(0,0) = "B1" Then
            If Target.Value = "Delete" Then
                Rows("3:4").EntireRow.Hidden = True
            End If
        End If
    End Sub
    Note. If you want the procedure just to call Macro1, it isn't necessary to check that B1 is equal to "Delete" in both procedures, just one is sufficient.
    Last edited by Joe4; Jul 7th, 2017 at 08:58 AM.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    New Member
    Join Date
    Jul 2017
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    Hi Joe,

    Thanks for your reply. I've removed my code and just put this in. But nothing is happening. Have typed in delete, added it from a dropdown and no macro is being triggered. Is there something I'm missing here?

  4. #4
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    41,527
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    Did you place the code in the appropriate Sheet module (and not in a Standard module)?
    Worksheet_Change event procedures must be in the appropriate sheet module in order to run correctly.

    Also, have you disabled events anywhere along the way? If so, you will need to re-enable them, like with a short procedure like this:
    Code:
    Sub ReEnableEvents()
        Application.EnableEvents = True
    End Sub
    Just put that code in any module and run it to make sure events are enabled.

    Also, it shouldn't really matter, but the procedure name and Target are usually capitalized, i.e.
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) = "B1" Then
            If Target.Value = "Delete" Then
                Rows("3:4").EntireRow.Hidden = True
            End If
        End If
    End Sub
    Maybe some versions of Excel are sensitive to that.
    Last edited by Joe4; Jul 7th, 2017 at 09:12 AM.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  5. #5
    New Member
    Join Date
    Jul 2017
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    Hi Joe,

    Thanks again for your help. This has been so useful already. It was the enable events that was causing the issue.

    I just have one more question, this one might be slightly more complicated but hopefully you can help.

    What would I need to do to change the code so that:

    If the user selected "Delete" then it would hide the rows, if the user selected "Open" it would hide the rows. If anything else it would unhide the rows and bring them back.

    Thanks again!

  6. #6
    Board Regular
    Join Date
    Jan 2017
    Location
    Ukraine
    Posts
    210
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    Sorry to stick in, but try that
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) = "B1" Then
            Select Case Target
            Case "Delete", "Open"
                Rows("3:4").EntireRow.Hidden = True
            Else
                Rows("3:4").EntireRow.Hidden = False
            End Select
        End If
    End Sub

  7. #7
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    41,527
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    If I understand you correctly, maybe something like this:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) = "B1" Then
            If (Target.Value = "Delete") Or (Target.Value = "Open") Then
                Rows("3:4").EntireRow.Hidden = True
            Else
                Rows("3:4").EntireRow.Hidden = False
            End If
        End If
    End Sub
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  8. #8
    New Member
    Join Date
    Jul 2017
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    Thanks again Joe, I tried you method as well S_Wish and was getting a Complile Else with If Error. I've seen the case method on other forums so think it could work but appreciate your help.

    Joe- code worked perfectly and used it with multiple 'If' & 'Or' statements which is what I need

  9. #9
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    41,527
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

    I love Case statements!
    But I typically only use them when there are more than two possibilities. Otherwise, there isn't much of an advantage.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  10. #10
    New Member
    Join Date
    Jul 2017
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel Macro: Run Macro Based on a cell value

      
    Do you know why S_Wish code would provide an error using the Case statement?

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com