JohnPoole
Active Member
- Joined
- Jun 9, 2005
- Messages
- 267
Hi all, I was looking at an old thread from 2017:
,
This provided some code which seemed to work fine, I amended slightly so that it compares Column A in Sheet1 with Column A in Sheet2. If the value in Sheet1 is is unique and doesn't exist in Sheet2, then delete the row from Sheet1
This all seemed to work fine.... i added some other bits to output the values it was going to delete to the debug window etc and all was working well.... even on lists a couple of thousand rows long.
Until it stopped for no reason that I can see with error 9 - subscript out of Range on line 'For Each Cl In Sheets("Sheet2").Range("A2", Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp))' - and also caused the original un-modified code to break on longer lists also.
My modified code:
I don't understand why my modified code that was working now errors
I think it has something to do with the create object but not sure....
Can anybody explain why this works intermittently or is their a newer / better solution to identify unique entries and report them?
Thanks in advance,.
vba Delete row if value doesn't exist in a range
I have 2 worksheets (Sheet1 and sheet2). I am trying to write a macro that will look for the cell value of Sheet1 A2 in sheet2 column C. If that value doesn't exist in sheet2 column c then delete row 2 on sheet1. This needs to happen for all rows on sheet1. The amount of rows on sheet1 and...
www.mrexcel.com
This provided some code which seemed to work fine, I amended slightly so that it compares Column A in Sheet1 with Column A in Sheet2. If the value in Sheet1 is is unique and doesn't exist in Sheet2, then delete the row from Sheet1
VBA Code:
Sub DelRows()
Dim Cl As Range
Dim Rng As Range
With CreateObject("scripting.dictionary")
For Each Cl In Sheets("Sheet2").Range("A2", Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp))
If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1).Value
Next Cl
For Each Cl In Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp))
If Not .exists(Cl.Value) Then
If Rng Is Nothing Then
Set Rng = Cl
Else
Set Rng = Union(Rng, Cl)
End If
End If
Next Cl
End With
'Rng.EntireRow.Delete
End Sub
This all seemed to work fine.... i added some other bits to output the values it was going to delete to the debug window etc and all was working well.... even on lists a couple of thousand rows long.
Until it stopped for no reason that I can see with error 9 - subscript out of Range on line 'For Each Cl In Sheets("Sheet2").Range("A2", Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp))' - and also caused the original un-modified code to break on longer lists also.
My modified code:
VBA Code:
Sub DelRowsV1()
Dim Cl As Range
Dim Rng As Range
Dim MyAddress As Variant
With CreateObject("scripting.dictionary")
For Each Cl In Sheets("Sheet2").Range("C2", Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp))
If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1).Value
Next Cl
For Each Cl In Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp))
If Not .exists(Cl.Value) Then
If Rng Is Nothing Then
'Debug.Print C1
Set Rng = Cl
Else
'Debug.Print C1
Set Rng = Union(Rng, Cl)
'Debug.Print Rng.Address
MyAddress = Rng.Address
'Debug.Print MyAddress
End If
End If
Next Cl
End With
Dim arr() As String
Dim MyCell As Range
' Split the string to an array
MyAddress = Replace(MyAddress, ":", ",", 1, , vbTextCompare)
arr = Split(MyAddress, ",")
Dim name As Variant
For Each name In arr
Debug.Print "Sheet1!" & name
'TXT2RNG ("Sheet1!" & name)
Debug.Print "B " & Sheet1.Range(name).Value
Next
'Rng.EntireRow.Delete
End Sub
I don't understand why my modified code that was working now errors
I think it has something to do with the create object but not sure....
Can anybody explain why this works intermittently or is their a newer / better solution to identify unique entries and report them?
Thanks in advance,.