Results 1 to 5 of 5

Automatically copy data based on 2 criteria

Threaded View

OscarTheG Automatically copy data based... 10-12-2016, 05:03 AM
S O Re: Automatically copy data... 10-12-2016, 05:04 AM
OscarTheG Re: Automatically copy data... 10-12-2016, 06:11 AM
S O Re: Automatically copy data... 10-12-2016, 06:50 AM
OscarTheG Re: Automatically copy data... 10-14-2016, 04:46 AM
  1. #1
    Registered User
    Join Date
    10-11-2016
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    13

    Automatically copy data based on 2 criteria

    Hey,

    I'm very new to using VBA and macros, for the below I've been working with some code i found on Ron De Bruins site.

    I have 2 sheets one called Campaign Status and one called Post Campaign Tracker. When someone changes a value in column H on the Campaign Status to "Done" or "Proactive Done" I want certain cells from that row to automatically populate the Post Campaign Tracker. The Post Campaign Tracker is essentially a summary sheet of all done campaigns. Each time this happens I want it to only add new rows to the bottom or clear the existing data and repaste.

    I've attached a sample document with the main information. I want only cells A, C, D, G and K in each row to be copied over to the Post Campaign Tracker if "Done" or "Proactive Done" are selected in column H on the Campaign Status.

    Ron's code that I've been working with is below:

    [CODE][CODE][CODE][CODE][CODE]
    Sub PCT()
    'Note: This macro use the function LastRow
    'Important: The DestSh must exist
        Dim My_Range As Range
        Dim DestSh As Worksheet
        Dim CalcMode As Long
        Dim ViewMode As Long
        Dim FilterCriteria As String
        Dim CCount As Long
        Dim rng As Range
    
        'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
        'and the header of the first column, D is the last column in the filter range.
        'You can also add the sheet name to the code like this :
        'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
        'No need that the sheet is active then when you run the macro when you use this.
        Set My_Range = Worksheets("Campaign Status").Range("A1:K" & LastRow(Worksheets("Campaign Status")))
        My_Range.Parent.Select
    
        'Set the destination worksheet
        Set DestSh = Sheets("Post Campaign Tracker")
    
        If ActiveWorkbook.ProtectStructure = True Or _
           My_Range.Parent.ProtectContents = True Then
            MsgBox "Sorry, not working when the workbook or worksheet is protected", _
                   vbOKOnly, "Copy to new worksheet"
            Exit Sub
        End If
    
        'Change ScreenUpdating, Calculation, EnableEvents, ....
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        ActiveSheet.DisplayPageBreaks = False
    
        'Firstly, remove the AutoFilter
        My_Range.Parent.AutoFilterMode = False
    
        'Filter and set the filter field and the filter criteria :
        'This example filter on the first column in the range (change the field if needed)
        'In this case the range starts in A so Field 1 is column A, 2 = column B, ......
        'Use "<>Netherlands" as criteria if you want the opposite
        My_Range.AutoFilter Field:=8, Criteria1:="=Done"
    
        'If you want to filter on a cell value you can use this, use "<>" for the opposite
        'This example uses the activecell value
        'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value
    
        'This will use the cell value from A2 as criteria
        'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value
    
        ''If you want to filter on a Inputbox value use this
        'FilterCriteria = InputBox("What text do you want to filter on?", _
         '                          "Enter the filter item.")
        'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria
    
    
        'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
        CCount = 0
        On Error Resume Next
        CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
        On Error GoTo 0
        If CCount = 0 Then
            MsgBox "There are more than 8192 areas:" _
                 & vbNewLine & "It is not possible to copy the visible data." _
                 & vbNewLine & "Tip: Sort your data before you use this macro.", _
                   vbOKOnly, "Copy to worksheet"
        Else
            'Copy the visible data and use PasteSpecial to paste to the Destsh
            With My_Range.Parent.AutoFilter.Range
                On Error Resume Next
                ' Set rng to the visible cells in My_Range without the header row
                Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
                          .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
                If Not rng Is Nothing Then
                    'Copy and paste the cells into DestSh below the existing data
                    rng.Copy
                    With DestSh.Range("A" & LastRow(DestSh) + 1)
                        ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
                        ' Remove this line if you use Excel 97
                        .PasteSpecial Paste:=8
                        .PasteSpecial xlPasteValues
                        .PasteSpecial xlPasteFormats
                        Application.CutCopyMode = False
                    End With
                    'Delete the rows in the My_Range.Parent worksheet
                    'rng.EntireRow.Delete
                End If
            End With
        End If
    
        'Close AutoFilter
        My_Range.Parent.AutoFilterMode = False
    
        'Restore ScreenUpdating, Calculation, EnableEvents, ....
        ActiveWindow.View = ViewMode
        Application.Goto DestSh.Range("A1")
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    
    End Sub
    
    
    Function LastRow(sh As Worksheet)
        On Error Resume Next
        LastRow = sh.Cells.Find(What:="*", _
                                After:=sh.Range("A1"), _
                                Lookat:=xlPart, _
                                LookIn:=xlValues, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, _
                                MatchCase:=False).Row
        On Error GoTo 0
    End Function

    To get this code to run automatically from the Campaign Status I've added in the below code to that sheet. It includes a datepicker and I can't seem to get the macro that makes the above run automatically to work with the datepicker.

    Private Sub DTPicker1_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)
    
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    ' Developed by Contextures Inc.
    ' www.contextures.com
          Dim KeyCells As Range
    
        ' The variable KeyCells contains the cells that will
        ' cause an alert when they are changed.
        Set KeyCells = Range("H:H")
        
        If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
    
            Call PCT
           
        End If
    
    Dim rngDV As Range
    Dim oldVal As String
    Dim newVal As String
    If Target.Count > 1 Then GoTo exitHandler
    
    On Error Resume Next
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
    On Error GoTo exitHandler
    
    If rngDV Is Nothing Then GoTo exitHandler
    
    If Intersect(Target, rngDV) Is Nothing Then
       'do nothing
    Else
      Application.EnableEvents = False
      newVal = Target.Value
      Application.Undo
      oldVal = Target.Value
      Target.Value = newVal
      If Target.Column = 5 _
      Or Target.Column = 12 _
       Or Target.Column = 13 Then
        If oldVal = "" Then
          'do nothing
          Else
          If newVal = "" Then
          'do nothing
          Else
          Target.Value = oldVal _
            & ", " & newVal
    '      NOTE: you can use a line break,
    '      instead of a comma
    '      Target.Value = oldVal _
    '        & Chr(10) & newVal
          End If
        End If
      End If
    End If
    
    
    exitHandler:
      Application.EnableEvents = True
    End Sub
    Any help would be much appreciated, this is for my work.

    Thanks

    Oscar
    Attached Files Attached Files
    Last edited by OscarTheG; 10-12-2016 at 06:11 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Automatically fill in data based on multiple criteria from another spreadsheet
    By TrinhTran02 in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 02-12-2015, 03:07 PM
  2. Automatically copying data into a new table based on 2 column criteria
    By gtdread in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-28-2014, 12:25 PM
  3. Replies: 19
    Last Post: 02-27-2014, 02:42 AM
  4. Replies: 2
    Last Post: 01-08-2014, 04:41 PM
  5. Replies: 5
    Last Post: 08-14-2013, 02:41 PM
  6. [SOLVED] Automatically populate a table with slected data based on criteria
    By smithrog in forum Excel General
    Replies: 7
    Last Post: 07-01-2013, 03:52 AM
  7. Automatically copy values based on unique/new criteria from a range
    By djarcadian in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-31-2013, 08:45 PM

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