+ Reply to Thread
Results 1 to 10 of 10

Combining two Private Sub Worksheet_Change

Hybrid View

  1. #1
    bdf0827
    Guest

    Combining two Private Sub Worksheet_Change

    I'm having some issues here. I have two Private Sub Worksheet_Change that I need in one worksheet. I can't figure out how to make it work with both private subs. Below is my coding. Any ideas would be so helpful!!

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    ' stanleydgromjr, 09/05/2011
    ' Version 3, after copying the Target.Row, delete the Target.Row
    ' http://www.excelforum.com/excel-programming/790860-move-entire-row-to-another-worksheet-based-on-cell-value.html
    If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    If Target = "" Then Exit Sub
    Dim P As Long, NR As Long
    With Application
      .EnableEvents = False
      .ScreenUpdating = False
      Select Case Target.Value
        Case "Booked"
          P = Application.Match("Potential", Worksheets("Booked").Columns(4), 0)
          NR = Worksheets("Booked").Range("D" & P).End(xlUp).Offset(1).Row
          Range("A" & Target.Row & ":J" & Target.Row).Copy Worksheets("Booked").Range("A" & NR)
          Rows(Target.Row).Delete
        Case "DNMQ"
          P = Application.Match("Potential", Worksheets("DNMQ").Columns(4), 0)
          NR = Worksheets("DNMQ").Range("D" & P).End(xlUp).Offset(1).Row
          Range("A" & Target.Row & ":J" & Target.Row).Copy Worksheets("DNMQ").Range("A" & NR)
          Rows(Target.Row).Delete
      End Select
      .EnableEvents = True
      .ScreenUpdating = True
    End With
    End Sub
    
    Private Sub Worksheet_Change1(ByVal Target As Range)
        SetDateRow Target, "G"
         'or
         'SetDateCol Target, 5
    End Sub
     
    Sub SetDateRow(Target As Range, Col As String)
        If Target.Cells.Count > 1 Then Exit Sub
        Application.EnableEvents = False
        Cells(Target.Row, Col) = Int(Now())
        Application.EnableEvents = True
    End Sub
     
    Sub SetDateCol(Target As Range, Rw As Long)
        If Target.Cells.Count > 1 Then Exit Sub
        Application.EnableEvents = False
        Cells(Rw, Target.Column) = Now()
        Application.EnableEvents = True
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Combining two Private Sub Worksheet_Change

    Try this:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim P           As Long
        Dim NR          As Long
    
        If Target.Cells.Count > 1 Then Exit Sub
        On Error GoTo Oops
        Application.EnableEvents = False
    
        Cells(Target.Row, "G") = Date
    
        If Target.Column = 4 Then
            Select Case Target.Value
                Case "Booked"
                    P = WorksheetFunction.Match("Potential", Worksheets("Booked").Columns(4), 0)
                    NR = Worksheets("Booked").Cells(P, "D").End(xlUp).Offset(1).Row
                    With Target.EntireRow
                        .Range("A1:J1").Copy Worksheets("Booked").Cells(NR, "A")
                        .Delete
                    End With
                Case "DNMQ"
                    P = WorksheetFunction.Match("Potential", Worksheets("DNMQ").Columns(4), 0)
                    NR = Worksheets("DNMQ").Cells(P, "D").End(xlUp).Offset(1).Row
                    With Target.EntireRow
                        .Range("A1:J1").Copy Worksheets("DNMQ").Cells(NR, "A")
                        .Delete
                    End With
            End Select
    Oops:
            Application.EnableEvents = True
        End If
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    bdf0827
    Guest

    Re: Combining two Private Sub Worksheet_Change

    That code didn't work for me. Let me tell you what it needs to do because the current code can be re-written probably. I'm not sure if its the most effective way or if its compatible with eachother.

    I'm going to attach the document and then explain what I'm trying to have happen.

    1) worksheet named current: when I change the status in status column to Booked - the entire row will be deleted and moved to the Booked worksheet.
    2) worksheet named current: when I change the status in the status column to DNMQ - the entire row will be deleted and moved to the DNMQ worksheet.
    3)worksheet named current: when I change the status in the status column to Application, I want todays date to appear in the App Date column (G). I don't want this date to change each time its updated or saved though. A static date if you will. Also, if I change from Approved to closing I dont want the date to change in the column G. Once its there I don't need it to change for tracking purposes.
    4) Finally, when there is a change in columns A-F I want another static date to appear in column J. This date wont change until their is any change in columns A-F.

    Any help or ideas would be so appreciated.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Combining two Private Sub Worksheet_Change

    Hello bdf0827,

    There is and always will be one and only one Worksheet Change event per worksheet. All the code therefore must be combined into the one event module.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    bdf0827
    Guest

    Re: Combining two Private Sub Worksheet_Change

    Right...? That's what I'm trying to figure out how to do. Not disputing the fact that there can be only one change event.




    Quote Originally Posted by Leith Ross View Post
    Hello bdf0827,

    There is and always will be one and only one Worksheet Change event per worksheet. All the code therefore must be combined into the one event module.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Combining two Private Sub Worksheet_Change

    That code didn't work for me.
    That's not very helpful. Did it do nothing? Something, but not what you wanted? In what way?

    Are you looking for someone to code to your specifications (aka, a consultant), or someone to help you help yourself?

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combining two Private Sub Worksheet_Change

    Find another forum then

    You haven't even used the suggested code
    Last edited by royUK; 11-14-2011 at 03:15 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  8. #8
    bdf0827
    Guest

    Thumbs down Re: Combining two Private Sub Worksheet_Change

    Really, Roy? Can you not read? I said it didn't work. Stop trolling. Troll!

    Quote Originally Posted by royUK View Post
    Find another forum then

    You haven't even used the suggested code

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combining two Private Sub Worksheet_Change

    It's not used in your attachment so how can we tell if you have used it correctly?

    Any way uou have the option to go

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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