VBA to delete row based on cell value

dumbitdown

New Member
Joined
Jul 23, 2007
Messages
28
I'm trying to get some code to help me delete rows from a data set when a value in column I = 2016, everything I've seen so far just ends up deleting all rows of data, now those that I've tried to specify as having 2016 in column I

There are several hundred rows of data and it gets refreshed with new stuff on a fairly regular basis so there is no defined volume of records in the list that is being used.

Would someone be able to provide me with some code to help with this?

Thanks
 
You may want to take a look at the links I provided, as I think the code provided here does not account for the header row (if there is one), and it will delete that too.

Trying to use them and can't seem to get them working... I'll keep tinkering and see what I can do

Cheers
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
If you still run into issues, please let us know the following so we can try to recreate the scenario on our side.
- What row does your data start on?
- Is there a header row?
- How many column of data do you have?
- Is the 2016 hard-coded or the result of a formula?
- Is the 2016 numeric, or a number entered in text format (usually indicated if the number is left-justified)?
 
Upvote 0
If you still run into issues, please let us know the following so we can try to recreate the scenario on our side.
- What row does your data start on?
- Is there a header row?
- How many column of data do you have?
- Is the 2016 hard-coded or the result of a formula?
- Is the 2016 numeric, or a number entered in text format (usually indicated if the number is left-justified)?

Thanks Joe4, think I've managed to resolve it, I used Macro recorder, however I sorted the data first prior to attempting to delete and that seems to have done the trick

True test will come when I have a refreshed data set to see if it works

Cheers
 
Upvote 0
Try this:

Code:
Sub FilterMini()
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "I").End(xlUp).Row
With ActiveSheet.Range("I1:I" & Lastrow)
.AutoFilter Field:=1, Criteria1:="2016", Operator:=xlFilterValues
.SpecialCells(xlCellTypeVisible).[B][COLOR="#FF0000"]Rows[/COLOR][/B].Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub
Shouldn't Rows above be EntireRow instead?
 
Last edited:
Upvote 0
No. I always just use "Rows" its impossible from what I understand to delete a partial row. We can delete a single cell. I never understand why people use "EntireRow"
It must be due to the combination of AutoFilter and xlCellTypeVisible why it works because if you use, for example, SpecialCells(xlCellTypeBlanks) on a single column with non-contiguous blank cells scattered about, and use Rows.Delete, you will only get those blank cells deleted and all data from the right moves over one column to fill the deleted cells... only using EntireRow gets the entire row deleted.
 
Last edited:
Upvote 0
When I use a script like this, It always deletes the entire row also:

Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "G").End(xlUp).Row
For i = Lastrow To 1 Step -1
    If Cells(i, "G").Value = "Tom" Then Rows(i).Delete
    Next
Application.ScreenUpdating = True
End Sub






It must be due to the combination of AutoFilter and xlCellTypeVisible why it works because if you use, for example, SpecialCells(xlCellTypeBlanks) on a single column with non-contiguous blank cells scattered about, and use Rows.Delete, you will only get those blank cells deleted and all data from the right moves over one column to fill the deleted cells... only using EntireRow gets the entire row deleted.
 
Last edited:
Upvote 0
When I use a script like this, It always deletes the entire row also:

Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "G").End(xlUp).Row
For i = Lastrow To 1 Step -1
    If Cells(i, "G").Value = "Tom" Then Rows(i).Delete
    Next
Application.ScreenUpdating = True
End Sub
That is different from what I was referring to... here is what I meant. Set this up...

ABCD
1AA1BB1CC1
2AA2BB2CC2
3AA3CC3CC3
4 1CC4
5 2CC5
6 3CC6
7AA8BB8CC7
8AA13CC13CC8
9 4CC9
10AA17BB17CC10
11 5CC11
12AA20 CC12
13 6CC13
14 7CC14
15AA26BB21CC15
16

<colgroup><col style="width: 30px; font-weight: bold;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"></colgroup><tbody>
</tbody>

Then run this macro on it (note my use of Rows)...
Code:
Sub Demo()
  Range("A1:A15").SpecialCells(xlCellTypeBlanks).[B][COLOR="#FF0000"]Rows[/COLOR][/B].Delete
End Sub
When you execute this code, the entire row will not be deleted, only the cells in Column A are deleted and the data in Columns B and C move over to fill the deleted cells... only by using EntireRow will the entire row be deleted. The reason I thought you needed to use EntireRow in your original code is because your deletion line is the same as in the above code except you used xlCellTypeVisible instead of xlCellTypeBlanks and you applied your code against filted data... my guess is that filtered data coupled with SpecialCells(xlCellTypeVisible) makes your use of Rows work..
 
Upvote 0
O yes. I see. Thanks for that info.
That is different from what I was referring to... here is what I meant. Set this up...

A
B
C
D
1
AA1
BB1
CC1
2
AA2
BB2
CC2
3
AA3
CC3
CC3
4
1
CC4
5
2
CC5
6
3
CC6
7
AA8
BB8
CC7
8
AA13
CC13
CC8
9
4
CC9
10
AA17
BB17
CC10
11
5
CC11
12
AA20
CC12
13
6
CC13
14
7
CC14
15
AA26
BB21
CC15
16

<tbody>
</tbody>

Then run this macro on it (note my use of Rows)...
Code:
Sub Demo()
  Range("A1:A15").SpecialCells(xlCellTypeBlanks).[B][COLOR=#ff0000]Rows[/COLOR][/B].Delete
End Sub
When you execute this code, the entire row will not be deleted, only the cells in Column A are deleted and the data in Columns B and C move over to fill the deleted cells... only by using EntireRow will the entire row be deleted. The reason I thought you needed to use EntireRow in your original code is because your deletion line is the same as in the above code except you used xlCellTypeVisible instead of xlCellTypeBlanks and you applied your code against filted data... my guess is that filtered data coupled with SpecialCells(xlCellTypeVisible) makes your use of Rows work..
 
Upvote 0

Forum statistics

Threads
1,217,371
Messages
6,136,178
Members
449,996
Latest member
duraichandra

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