VBA copy updates to new sheet

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet with columns A - AC. Column AC will be updated with "Red Flag" if criteria is not met. What I'm trying to do is copy any rows that were updated with Red Flag to a new sheet. Column E is the individuals ID #, so I was trying to copy based off columns E and AC. I'm not sure if this is possible. I want to add any new Red Flags to the end of the list on the 2nd sheet. Below is the code I tried but it's giving a 400 error. Any ideas?

Code:
Sub copyData_multiRef()

    Dim tWB As Workbook 'Thisworkbook
    Set tWB = ThisWorkbook
    
    Dim multiSH As Worksheet 'Thisworkbook MultiCol Sheet
    Set multiSH = tWB.Sheets("Notes")
    
    Dim filePath As String
    filePath = Application.GetOpenFilename
    
    Dim dataWB As Workbook 'Sample Data Workbook
    Set dataWB = Workbooks.Open(filePath)
    
    Dim dataSH As Worksheet 'Sample Data workbook ALLCOLUMN sheet
    Set dataSH = dataWB.Sheets("Red Flags")
    
    Dim a As Long, b As Long
    'Loop thru all the sample data records
    For a = 2 To dataSH.Range("A1").CurrentRegion.Rows.Count
        
        'Check if the records exists in the primary tool
        Dim recordExists As Boolean
        recordExists = False
        For b = 2 To multiSH.Range("A2").CurrentRegion.Rows.Count
            If dataSH.Range("E" & a).Value = multiSH.Range("E" & b).Value And _
                dataSH.Range("CA" & a).Value = multiSH.Range("CA" & b).Value Then
                recordExists = True
                Exit For
            End If
        Next b
        
        'Copy Data if not exists
        'Get the next avaialble row in the primary tool
        Dim nextRow As Long
        nextRow = multiSH.Range("A2").CurrentRegion.Rows.Count + 1
        If recordExists = False Then
            multiSH.Range("A:CA" & nextRow).Value = dataSH.Range("A:CA" & a).Value
        End If
    Next a
    
    'Close the Sample Data workbook
    dataWB.Close False
    
    MsgBox "Data copied successfully.", vbInformation, "VBA Copy New Data"
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Once you have identified the row you want to copy, something like this should work:

VBA Code:
Dim sourceRow As Range
Dim targetWorksheet As Worksheet
Dim targetRow As Range
    
' Set the source row as the active row
Set sourceRow = ActiveSheet.Rows(ActiveCell.Row)
    
' Set the target worksheet where you want to copy the row
Set targetWorksheet = ThisWorkbook.Worksheets("TargetSheetName")
    
' Find the next available row in the target worksheet
Set targetRow = targetWorksheet.Cells(targetWorksheet.Rows.Count, "A").End(xlUp).Offset(1)

' Copy the values and formats from the source row to the target row
sourceRow.Copy Destination:=targetRow

' Clear the clipboard
Application.CutCopyMode = False
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top