Results 1 to 5 of 5

Macro to Copy Coloured Data on "Import Templates last updated" created by CF

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Macro to Copy Coloured Data on "Import Templates last updated" created by CF

    I have the following Code below to copy data that is coloured in Cols A1 to last row in Col D on sheets "Import Templates last updated" created by CF
    and paste these on sheets "Reports Ready for checking"


    When running the code, the data in the last row in Col D and E is being removed

    Kindly test and amend my code that this does not happen




     sub CopyImportTemplatesUpdated()
        Dim importSheet As Worksheet
        Dim reportSheet As Worksheet
        Dim importRange As Range
        Dim cell As Range
        Dim lastRow As Long
        Dim startColumn As Long
        Dim j As Long
    
        ' Set the source and destination sheets
        Set importSheet = ThisWorkbook.Sheets("Import Templates last updated")
        Set reportSheet = ThisWorkbook.Sheets("Reports Ready for checking")
    
        ' Find the last row in column C of the source sheet
        lastRow = importSheet.Cells(importSheet.Rows.Count, "C").End(xlUp).Row
    
        ' Set the range to the columns A to C in the source sheet
        Set importRange = importSheet.Range("A1:C" & lastRow)
        startColumn = importRange.Columns(1).Column ' Get the starting column index
    
        ' Clear A1 to the last row in Column C in the destination sheet
        reportSheet.Range("A1:C" & reportSheet.Rows.Count).Clear
    
        ' Initialize a dictionary to keep track of unique rows
        Dim uniqueRows As Object
        Set uniqueRows = CreateObject("Scripting.Dictionary")
    
        ' Loop through each cell in the specified range
        For Each cell In importRange
            ' Check if the cell has a yellow background due to conditional formatting
            If cell.DisplayFormat.Interior.Color = RGB(255, 255, 0) Then
                ' Collect the row data in a temporary array
                Dim rowData() As Variant
                ReDim rowData(1 To 1, 1 To importRange.Columns.Count)
    
                ' Copy the cell values to the temporary array
                For j = 1 To importRange.Columns.Count
                    rowData(1, j) = importRange.Cells(cell.Row, j).Value
                Next j
    
                ' Generate a key for the row
                Dim rowKey As String
                rowKey = Join(Application.Index(rowData, 1, 0), "|")
    
                ' Check if the row is not already added
                If Not uniqueRows.Exists(rowKey) Then
                    ' Add the row to the destination sheet
                    reportSheet.Range("A" & reportSheet.Rows.Count).End(xlUp).Offset(1, 0).Resize(1, UBound(rowData, 2)).Value = rowData
                    ' Add the row to the dictionary to mark it as added
                    uniqueRows(rowKey) = True
                End If
            End If
        Next cell
    
        ' Delete the first row in the destination sheet
        reportSheet.Range("A1").EntireRow.Delete
    
        ' Autofit columns A to C in the destination sheet
        reportSheet.Range("A:D").EntireColumn.AutoFit
        MsgBox "Templates Updated have been Copied to sheet ""Reports Ready for checking""", vbInformation
        Sheets("Reports Ready for checking").Activate
       
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Master data to be updated from import sheet.
    By SvenOpolcer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-25-2021, 07:20 AM
  2. Newbie looking for help on these two templates I created
    By louis2008 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 10-01-2017, 02:26 AM
  3. [SOLVED] How to create macro to copy select data, or row into newly created tabs
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-14-2013, 12:01 PM
  4. Using macro to copy paste data from updated report into a database file
    By Adamlearnexcel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-12-2012, 02:50 AM
  5. [SOLVED] Want to write a macro which will copy only updated data
    By ahsanzafar in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-28-2012, 03:38 PM
  6. Macro to Copy coloured cells and paste in different worksheets
    By bigdee008@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2012, 09:44 AM
  7. how to new(Updated) Sql Data import in excel using vba
    By iamsvs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2011, 11:57 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