Results 1 to 6 of 6

Thread: Combine two Private Sub Worksheet_Change formulas
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Aug 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Combine two Private Sub Worksheet_Change formulas

    Good morning,

    I am in need of combining two Private Sub formulas together.

    Individually they work, and are as follows:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$F$2" Then
    Range("F3").Value = ""
    End If
    If Target.Address = "$F$3" Then
    Range("F4").Value = ""
    End If
    End Sub


    AND...


    Private Sub Worksheet_Change(ByVal Target As Range)
    Sheets("Sheet3").AutoFilter.ApplyFilter
    End Sub


    I thought combining them would be pretty simple, and I've tried a few different tweaks, but I always get an error.

    Any help would be greatly appreciated, so much.

  2. #2
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    50,799
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Combine two Private Sub Worksheet_Change formulas

    Welcome to the Board!

    Did you try:

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    '   Block 1
        If Target.Address = "$F$2" Then
            Range("F3").Value = ""
        End If
        If Target.Address = "$F$3" Then
            Range("F4").Value = ""
        End If
        
    '   Block 2
        Sheets("Sheet3").AutoFilter.ApplyFilter
    
    End Sub
    Also note, that the way you currently have it written, and change to cell F2 will clear both F3 and F4.
    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
    Aug 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Combine two Private Sub Worksheet_Change formulas

    Good morning -- and thank you!

    I did try your suggestion, again just now and before, and it doesn't seem to work as intended.
    I do need both If statements in the first formula -- as I need a change in F2 to clear out F3 and F4, and any change to F3 (separate and apart from a change to F2, perhaps) to clear out F4.

    And then I essentially want any change to the data to be filtered automatically, pursuant to filters that currently exist on the sheet.


    I appreciate your help, and I hope we can get this ironed out.

    cpe

  4. #4
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    50,799
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Combine two Private Sub Worksheet_Change formulas

    How are cells F2 and F3 being updated? Manually, or in another manner?
    Are you only ever updating one cell at a time (i.e. not copying and paste a WHOLE RANGE of cells at once)?
    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
    Board Regular
    Join Date
    Apr 2010
    Posts
    167
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Combine two Private Sub Worksheet_Change formulas

    Quote Originally Posted by cevors View Post
    Good morning -- and thank you!

    I did try your suggestion, again just now and before, and it doesn't seem to work as intended.
    I do need both If statements in the first formula -- as I need a change in F2 to clear out F3 and F4, and any change to F3 (separate and apart from a change to F2, perhaps) to clear out F4.

    And then I essentially want any change to the data to be filtered automatically, pursuant to filters that currently exist on the sheet.


    I appreciate your help, and I hope we can get this ironed out.

    cpe

    How about this?


    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$F$2" Then
            Range("F3").Value = ""
        End If
        If Target.Address = "$F$3" Then
            Range("F4").Value = ""
        End If
        With Sheets("Sheet1")
            Select Case .AutoFilterMode
                Case False
                    .Cells(1, .UsedRange.Columns.Count).Select
                    Selection.AutoFilter
            End Select
        End With
    End Sub

  6. #6
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    50,799
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Combine two Private Sub Worksheet_Change formulas

    Also, which Sheet are you on? In the initial post, it had a reference to Sheet3.
    Are the filters on the same sheet you are making the updates to?
    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!"

Some videos you may like

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
  •