VBA Loop Deleting row/range issue

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I have a loop issue.
I have a worksheet which I am preparing to be added to a database.
I need to delete the rows where Column A matches Column C.
Column D is just there while I am working on this.
NB. I have 3 floating buttons so I would prefer if I could delete the row from column A to D only.

The first code works highlighting the files to be deleted but I cannot get it to work when I change the instruction to delete.
This works
VBA Code:
Sub Matches()

Dim lastRow, i As Long
lastRow = ThisWorkbook.Sheets("ImportRINS").Cells(Rows.Count, "A").End(xlUp).Row
'On Error Resume Next
    For i = 2 To lastRow
        If Cells(i, 1).Value = Cells(i, 3).Value Then
        Rows(i).Interior.ColorIndex = 4
        End If
    Next i
End Sub
This doesn't work
VBA Code:
Sub Matches()

Dim lastRow, i As Long
lastRow = ThisWorkbook.Sheets("ImportRINS").Cells(Rows.Count, "A").End(xlUp).Row
'On Error Resume Next
    For i = 2 To lastRow
        If Cells(i, 1).Value = Cells(i, 3).Value Then
        Range(i, 4).Delete Shift:=xlToUpt  'I also tried    Rows(i).EntireRow.Delete       Range.Cells(i).EntireRow.Delete   with and without i=i-1 after this row
        End If
    Next i
End Sub

Data_Entry_Form_ver_25d.xlsm
ABCD
2David (b. 1950)16895FALSE
3Elizabeth (b. 1965)16902FALSE
4Braga, Jose Maria (b. 1897)16898Braga, Jose Maria (b. 1897)TRUE
5Cairnduff, Cyril Wilberforce (b. 1898)16890Cairnduff, Cyril Wilberforce (b. 1898)TRUE
6Cairnduff, Gwendoline May (b. 1920)16887Cairnduff, Gwendoline May (b. 1920)TRUE
7Coates, Doris Maude (b. 1900)16909Coates, Doris Maude (b. 1899)FALSE
8da Luz, Augusta da Conceição Ozorio (b. 1898)16899da Luz, Augusta Isabel da Conceição Ozorio (b. 1898)FALSE
9Donnelly, Matthew (b. 1884)16888FALSE
ImportRINS
Cell Formulas
RangeFormula
D2:D9D2=A2=C2


Thanks for your help.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Second code is fine EXCEPT loop criteria.
When you delete rows you have to delete them from 'bottom', so from lastRow to 2:

VBA Code:
Dim lastRow, i As Long
lastRow = ThisWorkbook.Sheets("ImportRINS").Cells(Rows.Count, "A").End(xlUp).Row
'On Error Resume Next
    For i = lastRow To 2 Step -1
        If Cells(i, 1).Value = Cells(i, 3).Value Then
        Rows(i).EntireRow.Delete
        End If
    Next i
should works.
 
Upvote 0
Solution
Second code is fine EXCEPT loop criteria.
When you delete rows you have to delete them from 'bottom', so from lastRow to 2:

VBA Code:
Dim lastRow, i As Long
lastRow = ThisWorkbook.Sheets("ImportRINS").Cells(Rows.Count, "A").End(xlUp).Row
'On Error Resume Next
    For i = lastRow To 2 Step -1
        If Cells(i, 1).Value = Cells(i, 3).Value Then
        Rows(i).EntireRow.Delete
        End If
    Next i
should works.
Thanks you ?.
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,242
Members
453,026
Latest member
cknader

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