How to combine two Private Sub Worksheet_Change for one sheet

WITRONNN

New Member
Joined
Feb 21, 2023
Messages
10
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello, I'm looking for two Private Sub Worksheet_Change for one sheet.
I need these to both to work, but can only get the first one to work.
Is there a way to get these to work by combining them somehow?

Private Sub Worksheet_Change(ByVal Target As Range)
' Auto Date
Dim Cell As Range
For Each Cell In Target
If Cell.Column = Range("I:I").Column Then
If Cell.Value <> "" Then
Cells(Cell.Row, "D").Value = Now
Else
Cells(Cell.Row, "D").Value = ""
End If
End If
Next Cell
End Sub


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
If Target.Address <> "$A$1" Then Exit Sub
Set rng = Cells.Find(What:=Target.Value, After:=Target, LookIn:=xlValues, LookAt:=xlWhole)
If rng.Address = Target.Address Then
MsgBox "Not found!", vbOKOnly, "Search for '" & Target.Value & "'"
Exit Sub
End If
rng.Activate
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the Board!

Just stack them, one over the other in a single Worksheet_Change procedure.
Also note, when updating cells within a single Worksheet_Change procedure, it is best to temporarily disable events from firing while making cell updates, or else the changes in the code will trigger another change and another round to run (people have gotten caught in infinite loops this way!).

Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'***FIRST PROCEDURE***
'   Auto Date
    Dim Cell As Range
    For Each Cell In Target
        If Cell.Column = Range("I:I").Column Then
            Application.EnableEvents = False
            If Cell.Value <> "" Then
                Cells(Cell.Row, "D").Value = Now
            Else
                Cells(Cell.Row, "D").Value = ""
            End If
            Application.EnableEvents = True
        End If
    Next Cell
 
'***SECOND PROCEDURE***
    Dim rng As Range
    If Target.Address <> "$A$1" Then Exit Sub
        Set rng = Cells.Find(What:=Target.Value, After:=Target, LookIn:=xlValues, LookAt:=xlWhole)
        If rng.Address = Target.Address Then
            MsgBox "Not found!", vbOKOnly, "Search for '" & Target.Value & "'"
        Exit Sub
    End If
    rng.Activate
   
End Sub

Also, when posting VBA code in the future, please use Code tags as shown here: How to Post Your VBA Code
It makes your code much easier to read and work with!
 
Upvote 1
Solution
Welcome to the Board!

Just stack them, one over the other in a single Worksheet_Change procedure.
Also note, when updating cells within a single Worksheet_Change procedure, it is best to temporarily disable events from firing while making cell updates, or else the changes in the code will trigger another change and another round to run (people have gotten caught in infinite loops this way!).

Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'***FIRST PROCEDURE***
'   Auto Date
    Dim Cell As Range
    For Each Cell In Target
        If Cell.Column = Range("I:I").Column Then
            Application.EnableEvents = False
            If Cell.Value <> "" Then
                Cells(Cell.Row, "D").Value = Now
            Else
                Cells(Cell.Row, "D").Value = ""
            End If
            Application.EnableEvents = True
        End If
    Next Cell
 
'***SECOND PROCEDURE***
    Dim rng As Range
    If Target.Address <> "$A$1" Then Exit Sub
        Set rng = Cells.Find(What:=Target.Value, After:=Target, LookIn:=xlValues, LookAt:=xlWhole)
        If rng.Address = Target.Address Then
            MsgBox "Not found!", vbOKOnly, "Search for '" & Target.Value & "'"
        Exit Sub
    End If
    rng.Activate
  
End Sub

Also, when posting VBA code in the future, please use Code tags as shown here: How to Post Your VBA Code
It makes your code much easier to read and work with!
Thank you!
This worked perfectly.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
funcionará para tres listas desplegables (A, B, C) donde cambiar A anula B y C y cambiar B anula C
 
Upvote 0
funcionará para tres listas desplegables (A, B, C) donde cambiar A anula B y C y cambiar B anula C
This reply seems out of place. It seems to have nothing to do with the question, and is in a different language.
If you have a question, please post it to its own thread.
And if you would like to post in a language other than English, please post it here: Questions in Other Languages
 
Upvote 0
Sorry for the language error, it will work for three dropdown lists (D13, G13, I13) where changing the D13 list will clear the G13 and I13 lists and changing the G13 list will clear the I13 list try this code

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("D13").Address Then
Range("G13").Value = ""
End If


but when executing it tells me that there is an ambiguous name
 
Upvote 0
Sorry for the language error, it will work for three dropdown lists (D13, G13, I13) where changing the D13 list will clear the G13 and I13 lists and changing the G13 list will clear the I13 list try this code

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("D13").Address Then
Range("G13").Value = ""
End If


but when executing it tells me that there is an ambiguous name
I am not quite clear on if you are asking a question about this particular thread, if you have your own new question (in which case you should post it in a new thread), or you are trying to add assistance to this already solved question.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top