Hi all,
Looking to refine a macro that I have been using for some time now and don't know how to make the required changes and appreciate the assistance.
The current macro looks in Sh1 as searchsheet Column B for the manifest numbers and deletes any duplicates from Sh2 targetsheet prior to another macro copying them over.
What I'm trying to achieve is for the macro the search for duplicates in both column B & D as column B may have the same manifest number, but column D will contain a different item number and will need copying across.
I only want to delete the row if Columns B&D are the same in both sheets.
I have tried several times to make this work but can't seem to manage it, help would be greatly appreciated.
Thanks Brad
Sub CleanDupes()
Dim targetRange As Range, searchRange As Range
Dim targetArray
Dim x As Long
Application.ScreenUpdating = False
Dim TargetSheetName As String: TargetSheetName = ("Sh2")
Dim TargetSheetColumn As String: TargetSheetColumn = "B"
Dim SearchSheetName As String: SearchSheetName = ("Sh1")
Dim SearchSheetColumn As String: SearchSheetColumn = "B"
'Load target array
With Sheets(TargetSheetName)
Set targetRange = .Range(.Range(TargetSheetColumn & "1"), _
.Range(TargetSheetColumn & Rows.Count).End(xlUp))
targetArray = targetRange
End With
'Get Search Range
With Sheets(SearchSheetName)
Set searchRange = .Range(.Range(SearchSheetColumn & "1"), _
.Range(SearchSheetColumn & Rows.Count).End(xlUp))
End With
If IsArray(targetArray) Then
For x = UBound(targetArray) To 1 Step -1
If Application.WorksheetFunction.CountIf(searchRange, _
targetArray(x, 1)) Then
targetRange.Cells(x).EntireRow.Delete
End If
Next
Else
If Application.WorksheetFunction.CountIf(searchRange, targetArray) Then
targetRange.EntireRow.Delete
End If
End If
Application.ScreenUpdating = True
Call Transfer
End Sub
Looking to refine a macro that I have been using for some time now and don't know how to make the required changes and appreciate the assistance.
The current macro looks in Sh1 as searchsheet Column B for the manifest numbers and deletes any duplicates from Sh2 targetsheet prior to another macro copying them over.
What I'm trying to achieve is for the macro the search for duplicates in both column B & D as column B may have the same manifest number, but column D will contain a different item number and will need copying across.
I only want to delete the row if Columns B&D are the same in both sheets.
I have tried several times to make this work but can't seem to manage it, help would be greatly appreciated.
Thanks Brad
Sub CleanDupes()
Dim targetRange As Range, searchRange As Range
Dim targetArray
Dim x As Long
Application.ScreenUpdating = False
Dim TargetSheetName As String: TargetSheetName = ("Sh2")
Dim TargetSheetColumn As String: TargetSheetColumn = "B"
Dim SearchSheetName As String: SearchSheetName = ("Sh1")
Dim SearchSheetColumn As String: SearchSheetColumn = "B"
'Load target array
With Sheets(TargetSheetName)
Set targetRange = .Range(.Range(TargetSheetColumn & "1"), _
.Range(TargetSheetColumn & Rows.Count).End(xlUp))
targetArray = targetRange
End With
'Get Search Range
With Sheets(SearchSheetName)
Set searchRange = .Range(.Range(SearchSheetColumn & "1"), _
.Range(SearchSheetColumn & Rows.Count).End(xlUp))
End With
If IsArray(targetArray) Then
For x = UBound(targetArray) To 1 Step -1
If Application.WorksheetFunction.CountIf(searchRange, _
targetArray(x, 1)) Then
targetRange.Cells(x).EntireRow.Delete
End If
Next
Else
If Application.WorksheetFunction.CountIf(searchRange, targetArray) Then
targetRange.EntireRow.Delete
End If
End If
Application.ScreenUpdating = True
Call Transfer
End Sub