+ Reply to Thread
Results 1 to 15 of 15

two Private Sub Worksheet_Change in on one sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    two Private Sub Worksheet_Change in on one sheet

    Hi..

    I have two macros i want to be activated by changes made on sheet..
    i know they both work separately, but i cant get them to work together

    the first is tricked by changes in column C:C
    the other is tricked if there is entered and "X" in F:F

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim strAddress As String
        Dim val
        Dim dtmTime As Date
        Dim Rw As Long
         
        If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub
        
        dtmTime = Now()
        val = Target.Value
        strAddress = Target.Address
        
        Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
        With Sheets("Log Sheet")
            .Cells(Rw, 1) = strAddress
            .Cells(Rw, 2) = val
            .Cells(Rw, 3) = dtmTime
        End With
    End Sub
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Application.ScreenUpdating = False
      
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    Dim i As Integer
    
    Dim Msg As String, Ans As Variant
    
    i = Worksheets("Historik").Range("A1").CurrentRegion.Rows.Count
    For x = 1 To 9999
       
    If Cells(2 + x, 6) = "x" Then
    If MsgBox("Er du sikker på at du vil flytte denne til historik?", vbYesNo, "Overfør til historik?") = vbYes Then
        z = z + 1
        Worksheets("Historik").Cells(i + z, 1).Value = Cells(2 + x, 1)
        Worksheets("Historik").Cells(i + z, 2).Value = Cells(2 + x, 2)
        Worksheets("Historik").Cells(i + z, 3).Value = Cells(2 + x, 3)
        Worksheets("Historik").Cells(i + z, 4).Value = Cells(2 + x, 5)
    
    Call UpdateAction
    
    End If
    End If
    
    Next
    
    Call slet_x
    
    Application.ScreenUpdating = True
    
    End Sub
    Can any on help?

  2. #2
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    Copy everything in between Private Sub ... End Sub of the 2nd code and paste it at the end of the 1st code.

    Then change the If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub to
    If Intersect(Target, Range("C:C")) Is Nothing Then
    ...
    End If


    e.g. (untested)
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim strAddress As String
        Dim val
        Dim dtmTime As Date
        Dim Rw As Long
         
        If Intersect(Target, Range("C:C")) Is Nothing Then
        
            dtmTime = Now()
            val = Target.Value
            strAddress = Target.Address
            
            Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
            With Sheets("Log Sheet")
                .Cells(Rw, 1) = strAddress
                .Cells(Rw, 2) = val
                .Cells(Rw, 3) = dtmTime
            End With
        End If
        
        Application.ScreenUpdating = False
      
        Dim x As Integer
        Dim y As Integer
        Dim z As Integer
        Dim i As Integer
        
        Dim Msg As String, Ans As Variant
        
        i = Worksheets("Historik").Range("A1").CurrentRegion.Rows.Count
        For x = 1 To 9999
           
        If Cells(2 + x, 6) = "x" Then
        If MsgBox("Er du sikker på at du vil flytte denne til historik?", vbYesNo, "Overfør til historik?") = vbYes Then
            z = z + 1
            Worksheets("Historik").Cells(i + z, 1).Value = Cells(2 + x, 1)
            Worksheets("Historik").Cells(i + z, 2).Value = Cells(2 + x, 2)
            Worksheets("Historik").Cells(i + z, 3).Value = Cells(2 + x, 3)
            Worksheets("Historik").Cells(i + z, 4).Value = Cells(2 + x, 5)
        
        Call UpdateAction
        
        End If
        End If
        
        Next
        
        Call slet_x
        
        Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    it dosent work.. but maybe the macro with C:C is wrong?
    I want it to trick if i change or enter a date in C:C

  4. #4
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    Ahh, sorry, missed a step. Added a "Not" at the If statement.

    e.g. (untested)
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim strAddress As String
        Dim val
        Dim dtmTime As Date
        Dim Rw As Long
         
        If Not Intersect(Target, Range("C:C")) Is Nothing Then
        
            dtmTime = Now()
            val = Target.Value
            strAddress = Target.Address
            
            Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
            With Sheets("Log Sheet")
                .Cells(Rw, 1) = strAddress
                .Cells(Rw, 2) = val
                .Cells(Rw, 3) = dtmTime
            End With
        End If
        
        Application.ScreenUpdating = False
      
        Dim x As Integer
        Dim y As Integer
        Dim z As Integer
        Dim i As Integer
        
        Dim Msg As String, Ans As Variant
        
        i = Worksheets("Historik").Range("A1").CurrentRegion.Rows.Count
        For x = 1 To 9999
           
        If Cells(2 + x, 6) = "x" Then
        If MsgBox("Er du sikker på at du vil flytte denne til historik?", vbYesNo, "Overfør til historik?") = vbYes Then
            z = z + 1
            Worksheets("Historik").Cells(i + z, 1).Value = Cells(2 + x, 1)
            Worksheets("Historik").Cells(i + z, 2).Value = Cells(2 + x, 2)
            Worksheets("Historik").Cells(i + z, 3).Value = Cells(2 + x, 3)
            Worksheets("Historik").Cells(i + z, 4).Value = Cells(2 + x, 5)
        
        Call UpdateAction
        
        End If
        End If
        
        Next
        
        Call slet_x
        
        Application.ScreenUpdating = True
    
    End Sub
    [/QUOTE]

  5. #5
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    one more question
    Can i add a .Cells(Rw, 4) = the thing written in A

    maybe you know how to do that

  6. #6
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    Thanks a lot! it works just like i want i to !

  7. #7
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    Not a problem. Thanks for the rep!

  8. #8
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    thing written in A? as in, for example, of Rw = 100, cell D100 takes the same value as A100?

    If so, .Cells(Rw, 4) = .Cells(Rw,1)

  9. #9
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    In fx. D100 i want it to type the same value as A100 from the sheet which the macro is in.. now i takes the value from "Log Sheet"

  10. #10
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    yes.. but it takes the value from the wrong sheet..

  11. #11
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    Ahh, got it. Try this instead:


    .Cells(Rw, 4) = Cells(Rw,1)
    I removed a dot before Cells(Rw,1)

  12. #12
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    it dosent return anything :/ hmm

  13. #13
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    I think I know what you're trying to do. You want to insert the value in column A of the cell that changed. Try this

    .Cells(Rw, 4) = Cells(Target.Row,1)

  14. #14
    Forum Contributor
    Join Date
    10-23-2014
    Location
    Kolding, Denmark
    MS-Off Ver
    MS Office 2010
    Posts
    169

    Re: two Private Sub Worksheet_Change in on one sheet

    Thanks!! :d :d

  15. #15
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: two Private Sub Worksheet_Change in on one sheet

    You're welcome. Thanks for the rep!

+ 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. Private Sub Worksheet_Change problem
    By Delorean14 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2013, 02:25 AM
  2. How do I include two Private Sub Worksheet_Change’s
    By Jessy01 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-16-2013, 12:24 PM
  3. Combining two Private Sub Worksheet_Change
    By bdf0827 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-14-2011, 08:58 AM
  4. Private Sub Worksheet_Change:MI Summary.
    By Skybeau in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2010, 02:04 AM
  5. Private Sub Worksheet_Change
    By VicM in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2008, 01:28 AM
  6. Private Sub Worksheet_Change:do i need to turn them into one?
    By alex1982 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2007, 06:43 PM
  7. Private Sub Worksheet_Change:MAIN sheet
    By NickySA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2006, 12:43 PM
  8. [SOLVED] Tab Names-Private Sub Worksheet_Change
    By Tom Hewitt in forum Excel General
    Replies: 2
    Last Post: 03-21-2005, 11:06 AM

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