Macro to find a cell value, select and delete, and loop thru all instances

tlc83

New Member
Joined
May 20, 2010
Messages
5
Hi,

I'm a VBA rookie who's banging his head on his desk w/this one:
(I'm running Excel 2007 on Win XP)

I have a worksheet that has data similar to this in Column A:

Data Type A
A
B
C

Data Type B
A
B
C

Data Type A
A
B
C
...and so on...

Note that in my data set I'm working with the A,B,C data rows are separated from the next instance of Data Type by a blank row.

I'm trying to create a macro that will loop through and find each instance of the value "Data Type A" and delete that row, as well as the data rows A,B,C etc below it, but leave Data Type B and its respective data rows in the spreadsheet.
Any help would be greatly appreciated.

Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi and welcome to the board!!
You really should consider dumping the Blank Rows!! For Data Analysis, Excel hates blank Rows. that said, this macro should get you started
Code:
Sub DeleteDT()
Dim cl As Range
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = LR To 2 Step -1
Set cl = Cells(i, "A")
If cl = "Data Type A" Then cl.Resize(4, 1).EntireRow.Delete
Next i
End Sub

It looks at Column "A"

HTH
lenze
 
Upvote 0
Hi,

I'm a VBA rookie who's banging his head on his desk w/this one:
(I'm running Excel 2007 on Win XP)

I have a worksheet that has data similar to this in Column A:

Data Type A
A
B
C

Data Type B
A
B
C

Data Type A
A
B
C
...and so on...

Note that in my data set I'm working with the A,B,C data rows are separated from the next instance of Data Type by a blank row.

I'm trying to create a macro that will loop through and find each instance of the value "Data Type A" and delete that row, as well as the data rows A,B,C etc below it, but leave Data Type B and its respective data rows in the spreadsheet.
Any help would be greatly appreciated.

Thanks!

For what it's worth:

Code:
Sub DeleteDataType()
Dim i As Long
Dim NEXTROW As Long
NEXTROW = Cells(Rows.Count, 1).End(xlDown).Row
For i = 1 To NEXTROW
    If Right(Range("A" & i).Value, 6) = "Type A" Then
        Range("A" & i, Range("A" & i + 4)).EntireRow.Delete shift:=xlUp
    End If
Next i
End Sub
 
Upvote 0
Hey guys,
Thanks for the responses - the macro works but doesn't quite solve the problem due to some add'l clarification needed in my data example. The rows of data under each data type isn't always necessarily 3 - it can vary from 2 to around 50. Is there a way to tweak it have it erase all rows under an individual data type when the number of rows is an unknown variable?

Thanks again!
 
Upvote 0
Yes, we can make use of the Blank Row to resize. Or, if the data types different from the "Data Type A" like numberic or formulas??
Can't do it now, I have to run. Maybe someone else can jump in!!
lenze
 
Upvote 0
Assuming you still have the Blank Cells
Code:
Sub DeleteDT()
Dim cl As Range
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
For I = LR To 2 Step -1
Set cl = Cells(I, "A")
If cl = "Data Type A" Then
   LR2 = cl.End(xlDown).Row
   cl.Resize(LR2 - cl.Row + 1, 1).EntireRow.Delete
End If
Next I
End Sub

lenze
 
Upvote 0
Oooh...nice! That works wonderfully.

As a moderate to advanced Excel user, but still a newb when it comes to VBA / macros, any recommendations for more the best way to increase my skill in creating macros?

Thanks again for your help.
 
Upvote 0
Oooh...nice! That works wonderfully.

As a moderate to advanced Excel user, but still a newb when it comes to VBA / macros, any recommendations for more the best way to increase my skill in creating macros?

Thanks again for your help.
All I know I learned from this Board and a few others!! With the exception of Excel Gurus Gone Wild, I have never read a Book on Excel. So, a few hints!!
1) Don't be afraid of books. Check out the MrExcel bookstore.
2) Make use of the Macro recorder. Yes it will record EVERY keystroke and mouse move and give you a lot on unneeded code, but it is handy for getting the proper syntax.
3) Look for post that are similar to something you are doing and take the suggested code and try to modify it on your own. That is a great way to delve into how a code really works
4) Ask your question on the board!!

lenze
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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