+ Reply to Thread
Results 1 to 2 of 2

How do I put 2 codes in VBA Worksheet_change

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2020
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    13

    How do I put 2 codes in VBA Worksheet_change

    Hi,

    I am a newbie to VBA.

    I have a production sheet with two VBA codes working perfectly fine seperatly.

    One code is populating automatic dates in column 3 (C) and in column 16 (P) based on inputs in Column 1, 12 and 15. How do I put below codes together in one worksheet. When I just add both it doesnt help.

    Here is the first code and its working perfectly fine if its added alone:


    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
    
    Then I have my second code which I also want to be in the same worksheet . This Code provides me a pop up message based on column 6. Code is also working perfectly alone. 
    
     
    
    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 alot!
    Last edited by Pepe Le Mokko; 02-19-2020 at 08:59 AM. Reason: Code tags again

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,464

    Re: How do I put 2 codes in VBA Worksheet_change

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    I did it for you this time. Please read forum rules. Thanks

+ 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. How to Merge Two Worksheet_Change Codes
    By merveileuse in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-09-2019, 04:56 AM
  2. Merging 2 Private Sub Worksheet_Change codes
    By anilpatni1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-01-2019, 10:08 PM
  3. How to compile two codes in Private Sub Worksheet_Change(ByVal Target As Range)
    By rajeev.raj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-19-2019, 01:51 PM
  4. Combine Two Private Sub Worksheet_Change codes in same worksheet
    By abhinavbinkar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-01-2019, 06:58 AM
  5. [SOLVED] Combine two Private Sub Worksheet_Change VBA codes
    By sloshpuppy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-13-2017, 04:55 AM
  6. Connecting two VBA codes (Private Sub Worksheet_Change(ByVal Target As Range)
    By TinRu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2014, 06:47 AM
  7. Combine two Worksheet_Change Codes
    By otherbobby in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-26-2014, 04:10 PM

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