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!
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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
 

John Davis

Well-known Member
Joined
Sep 11, 2007
Messages
3,457
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
 

tlc83

New Member
Joined
May 20, 2010
Messages
5
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!
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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
 

tlc83

New Member
Joined
May 20, 2010
Messages
5
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.
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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
 

Forum statistics

Threads
1,082,116
Messages
5,363,251
Members
400,722
Latest member
DrewPop24

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top