Multiple codes in one worksheet_change

Arnis

New Member
Joined
Feb 23, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am a newbie to VBA and are creating a production sheet. I have two VBA codes which needs to be applied into difference columns. The codes fullfill separate purpose and is working perfectly fine in seperate excel sheets. One code is for automatically providing me dates when specific cells are updated and the other is for message box pop up.

I need the codes in one sheet. How can I make these codes work in the same worksheet_change. Below is my two codes.

Here is the first code and its working perfectly fine if its added alone:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Application.EnableEvents = False
Cells(Target.Row, 4).Value = Date + Time
Application.EnableEvents = True
Else
If Target.Column = 12 Then
Application.EnableEvents = False
Cells(Target.Row, 16).Value = Date + Time
Application.EnableEvents = True
Else
If Target.Column = 15 Then
Application.EnableEvents = False
Cells(Target.Row, 16).Value = Date + Time
Application.EnableEvents = True
End If
End If
End If

End sub

MY SECOND CODE:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Const iDefaultDays As Integer = 84
Dim dtExpected As Date

On Error GoTo Terminate

If Target.Cells.Count > 6 Or Target.Column <> 6 Then GoTo Terminate
If Target.Value = "" Then
Target.Offset(0, 2).ClearContents
GoTo Terminate
End If

Application.EnableEvents = False

If Not IsDate(Target.Value) Then
MsgBox "Invalid value - please enter a date", vbExclamation + vbOKOnly
Target.ClearContents
Target.Select
Else
dtExpected = Target.Value + iDefaultDays
If MsgBox("Expected delivery date: " & dtExpected & String(2, vbCr) & "Accept this date?", vbYesNo + vbQuestion) = vbNo Then
dtExpected = InputBox("Manually enter expected delivery date", , dtExpected)
End If
Target.Offset(0, 2).Value = dtExpected
End If

Terminate:
If Err Then
Debug.Print "Error", Err.Number, Err.Description
Err.Clear
End If
Application.EnableEvents = True
End Sub

Thanks in advance
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Const iDefaultDays As Integer = 84
    Dim dtExpected As Date
   
    On Error GoTo ReEnable
    Application.EnableEvents = False
   
    If Target.Cells.Count = 1 Then
        Select Case Target.Column
            Case 1
                Cells(Target.Row, 4).Value = Now
               
            Case 12, 15
                Cells(Target.Row, 16).Value = Now
                   
             Case 6
                If Target.Value = "" Then
                    Target.Offset(0, 2).ClearContents
                ElseIf Not IsDate(Target.Value) Then
                    MsgBox "Please enter a date.", vbExclamation, "Invalid Entry"
                    Target.ClearContents
                    Target.Select
                Else
                    dtExpected = Target.Value + iDefaultDays
                    If MsgBox("Expected delivery date: " & dtExpected & String(2, vbCr) & "Accept this date?", vbYesNo + vbQuestion) = vbNo Then
                        dtExpected = InputBox("Manually enter expected delivery date", , dtExpected)
                    End If
                    Target.Offset(0, 2).Value = dtExpected
                End If
        End Select
    End If
   
ReEnable:
    If Err.Number <> 0 Then MsgBox Err.Description, vbCritical, "Error " & Err.Number
    Application.EnableEvents = True
   
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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