ShadowLady17
New Member
- Joined
- May 19, 2022
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
I have a macro that I copied from somewhere which sorts the data input from the import sheet to the sorted data sheet - to only copy new data, paste into a separate worksheet, and then delete those lines from the import sheet. However, I'd ideally like it to only delete rows where there is some sort of data in column A, so for rows where column A is blank, don't delete the rows. Is there any way to incorporate this into the existing macro below?
VBA Code:
Sub London_Copy_New_Data()
'Copy all new rows from one worksheet to another.
Dim importSheet, destinationSheet As Worksheet
Dim importLastRow, importColumnCheck, destinationColumnCheck, _
importStartRow, destinationStartRow, curRow, destinationLastRow As Integer
Dim dataToCheck As Variant
Dim rng, rDel As Range
' ------------------------------------------------------------------- '
' ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| '
' Change this section to work for your workbook.
' ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| '
' ------------------------------------------------------------------- '
'Set the worksheets
Set importSheet = Sheets("London Import") 'worksheet to copy data from
Set destinationSheet = Sheets("London Sorted Data") 'worksheet to paste new data
'Import data column to check
importColumnCheck = 18
'Destination data column to check
destinationColumnCheck = 18
'Start row on import sheet
importStartRow = 2
'Start row on destination sheet
destinationStartRow = 2
' ------------------------------------------------------------------- '
' ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| '
' ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| '
' ------------------------------------------------------------------- '
'Get last row from import worksheet
importLastRow = importSheet.Cells(Rows.Count, importColumnCheck).End(xlUp).Row
'Loop through range
For curRow = importStartRow To importLastRow
'Get data to check
dataToCheck = importSheet.Cells(curRow, importColumnCheck).Value
'Get last row from destination sheet
destinationLastRow = destinationSheet.Cells(Rows.Count, importColumnCheck).End(xlUp).Row
'Check for duplicate
With destinationSheet.Range(destinationSheet.Cells(destinationStartRow, destinationColumnCheck), destinationSheet.Cells(destinationLastRow, destinationColumnCheck))
Set rng = .Find(What:=dataToCheck, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
'Record already exists
'mark rows for deletion
If Not rDel Is Nothing Then
Set rDel = Union(Range("A" & curRow), rDel)
Else
Set rDel = Range("A" & curRow)
End If
Else
'New record, so copy it over
importSheet.Range("A" & curRow).EntireRow.Copy destinationSheet.Range("A" & destinationLastRow + 1)
'mark rows for deletion
If Not rDel Is Nothing Then
Set rDel = Union(Range("A" & curRow), rDel)
Else
Set rDel = Range("A" & curRow)
End If
End If
End With
Next curRow
'Delete rows that need to be deleted
'Un-comment the next line of code if you want to delete copied rows.
rDel.EntireRow.Delete
End Sub
Last edited by a moderator: