This question already has an answer here:
Hi have used some code from an answer to a question 'How do I delete duplicates between two excel sheets quickly vba' and tried to alter this code to suite my own VBA script. the code does delete rows the same amount as to what is in the array but it is just deleting the first 11 rows. I am fairly new to VBA and not completely understanding why it is doing this. Below is a copy of the script I am using.
Dim overLayWB As Workbook 'Overlay_workbook
Dim formattedWB As Workbook 'Formatted_workbook
Dim formattedWS As Worksheet 'Current active worksheet (Formatted)
Dim overLayWS As Worksheet 'Worksheet in OverLay
Dim lastRowFormatted As Long
Dim lastRowOverLay As Long
Dim targetArray, searchArray
Dim targetRange As Range
Dim x As Long
'Update these 4 lines if your target and search ranges change
Dim TargetSheetName As String: TargetSheetName = "Formatted"
Dim TargetSheetColumn As String: TargetSheetColumn = "G22"
Dim SearchSheetName As String: SearchSheetName = "Overlay"
Dim SearchSheetColumn As String: SearchSheetColumn = "G22"
'open Overlay workbook
Set overLayWB = Workbooks.Open("C:\Documents\Templates\Overlaye.xls") 'Path for workbook Overlay to copy from
Set formattedWS = Workbooks("Formatted").Sheets("DLT Formatted")
Set overLayWS = Workbooks("Overlay").Sheets("Overlay")
Set formattedWB = ThisWorkbook
'Load target array
With formattedWS
Set targetRange = .Range(.Range(TargetSheetColumn & "7"), _
.Range(TargetSheetColumn & Rows.Count).End(xlUp))
targetArray = targetRange
End With
'Load Search Array
With overLayWS
searchArray = .Range(.Range(SearchSheetColumn & "7"), _
.Range(SearchSheetColumn & Rows.Count).End(xlUp))
End With
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
'Populate dictionary from search array
If IsArray(searchArray) Then
For x = 1 To UBound(searchArray)
If Not dict.exists(searchArray(x, 1)) Then
dict.add searchArray(x, 1), 1
End If
Next
Else
If Not dict.exists(searchArray) Then
dict.add searchArray, 1
End If
End If
'Delete rows with values found in dictionary
If IsArray(targetArray) Then
'Step backwards to avoid deleting the wrong rows.
For x = UBound(targetArray) To 1 Step -1
If dict.exists(targetArray(x, 1)) Then
targetRange.Cells(x).EntireRow.Delete
End If
Next
Else
If dict.exists(targetArray) Then
targetRange.EntireRow.Delete
End If
End If
Can anyone help me with this it would be much appreicated, I have not altered the scripting correctly, or is it missing something?
This looks off:
with your supplied values it translates to:
I don't think that's what you intended and should throw an error.
It seems to have become almost received wisdom on this site that the task of deleting rows is best achieved by looping through a
Range
from bottom to top and deleting each individual row whenever criteria are met. Yet this is really quite an inefficient method. Compare these two snippets, for example:Output is as follows:
These results aren't surprising as it's probably fair to generalise that the greater the number of individual interactions with a
Worksheet
, the slower the programme will be.What's a shame is that some of the posts to do with removing duplicates go to great lengths to read
Worksheet
values and reference items into arrays in order to avoid excessive sheet interactions. And yet all of those efficiency gains are lost to inefficient row deletion. What's misleading is that these posts sometimes purport to be "quick".Some might argue that they want to carry out tasks on the
Worksheet
in between row deletions. However, the VBA ranges update their addresses in the same way that an Excel formula range does. Have a look at the code below for an example of this:Output is:
So the following code would still delete cells "A4" and "A6" and the original cells "A8" and "A10", for example:
For a practical application, the OP could genuinely answer the question of 'How do I delete duplicates between two excel sheets quickly vba'? with the following code:
In actual fact, there are also some misunderstandings about the role and function of variables in the OP's code, and other respondents have already pointed those out. However, in the interest of completeness, a correct reading routine for his/her two
Worksheets
might be something like the below: