how to delete a row in excel with a specific word using visual basic

M

mchen

Hi,

I have an excel file that always come with a line at the end with
"downloaded:" in the cell. How can I write a macro to search for that
cell and delete the row that cell is in?

Thanks for all your help!
 
J

James Wesley Blues Band

Any time you are in doubt on how to interact with excel, try recording a new macro and then physically do the steps you are trying to code. Stop the recording and then go look at the recorded macro

www.jameswesleybluesband.com
 
D

David McRitchie

Hi "mchen" [email protected],

Delete rows with "N" in Column 31 (col AE)
from my http://www.mvps.org/dmcritchie/excel/delempty.htm

Sub Delete_N_MarkedRows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim lastrow As Long, r As Long
lastrow = ActiveSheet.UsedRange.Rows.Count
For r = lastrow To 1 Step -1
If UCase(Cells(r, 31).Value) = "N" Then Rows(r).Delete
Next r
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

If the string can occur in any column you might
experiment with vba FIND

Here is a posting by Patric Molloy:
http://google.com/groups?selm=uhvafZVlCHA.216@tkmsftngp07

I would suggest using your name when posting, it is a lot more friendly
 
D

Don Guillett

try this if the column is column A
Sub deleterowword()
Rows(Columns(1).Find("downloaded").Row).Delete
End Sub
 
M

mchen

Hi David,

Thanks for all your help. I'm sorry, my name is Michelle, I don't
know how to use my name to post my messages.
I have another problem, wondering if you can help me.
I have an excel sheet that has cumulative data. I would like the
macro to clean up the sheet by searching on column B and delete all
the rows that do not have previous day's data.
So today is May 17th, I would like the macro to keep all of May 16th's
data. The date format is in M/D/YYYY.

Thanks again,
Michelle

David McRitchie said:
Hi "mchen" [email protected],

Delete rows with "N" in Column 31 (col AE)
from my http://www.mvps.org/dmcritchie/excel/delempty.htm

Sub Delete_N_MarkedRows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim lastrow As Long, r As Long
lastrow = ActiveSheet.UsedRange.Rows.Count
For r = lastrow To 1 Step -1
If UCase(Cells(r, 31).Value) = "N" Then Rows(r).Delete
Next r
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

If the string can occur in any column you might
experiment with vba FIND

Here is a posting by Patric Molloy:
http://google.com/groups?selm=uhvafZVlCHA.216@tkmsftngp07

I would suggest using your name when posting, it is a lot more friendly
---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

mchen said:
Hi,

I have an excel file that always come with a line at the end with
"downloaded:" in the cell. How can I write a macro to search for that
cell and delete the row that cell is in?

Thanks for all your help!
 
D

David McRitchie

Hi Michelle,

Sub Delete_2DayorOlder()
' This macro deletes all rows on the active worksheet
' having dates 2 or more days old in Column A
' no check for dates so basically any number that could
' represent a date less than the previous day.
Dim rng As Range
Set rng = Intersect(Range("A:A"), _
ActiveSheet.UsedRange.SpecialCells(xlConstants, xlNumbers))
Dim i As Long
For i = rng.Count To 1 Step -1
If rng.Item(i).Value < (Int(Now) - 1) Then rng.Item(i).EntireRow.Delete
Next i
End Sub

I don't know how to include name with email address from Google either,
but signature line at bottom works. Thanks.
---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

mchen said:
Hi David,

Thanks for all your help. I'm sorry, my name is Michelle, I don't
know how to use my name to post my messages.
I have another problem, wondering if you can help me.
I have an excel sheet that has cumulative data. I would like the
macro to clean up the sheet by searching on column B and delete all
the rows that do not have previous day's data.
So today is May 17th, I would like the macro to keep all of May 16th's
data. The date format is in M/D/YYYY.

Thanks again,
Michelle

David McRitchie said:
Hi "mchen" [email protected],

Delete rows with "N" in Column 31 (col AE)
from my http://www.mvps.org/dmcritchie/excel/delempty.htm

Sub Delete_N_MarkedRows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim lastrow As Long, r As Long
lastrow = ActiveSheet.UsedRange.Rows.Count
For r = lastrow To 1 Step -1
If UCase(Cells(r, 31).Value) = "N" Then Rows(r).Delete
Next r
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

If the string can occur in any column you might
experiment with vba FIND

Here is a posting by Patric Molloy:
http://google.com/groups?selm=uhvafZVlCHA.216@tkmsftngp07

I would suggest using your name when posting, it is a lot more friendly
---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

mchen said:
Hi,

I have an excel file that always come with a line at the end with
"downloaded:" in the cell. How can I write a macro to search for that
cell and delete the row that cell is in?

Thanks for all your help!
 
M

mchen

Hi David,

Thanks for all your help, the code works great!
Just wondering if you can explain to me in plain english what the code
is doing line by line, this way I can learn this instead of just
copying and pasting.

Thanks,
Michelle

David McRitchie said:
Hi Michelle,

Sub Delete_2DayorOlder()
' This macro deletes all rows on the active worksheet
' having dates 2 or more days old in Column A
' no check for dates so basically any number that could
' represent a date less than the previous day.
Dim rng As Range
Set rng = Intersect(Range("A:A"), _
ActiveSheet.UsedRange.SpecialCells(xlConstants, xlNumbers))
Dim i As Long
For i = rng.Count To 1 Step -1
If rng.Item(i).Value < (Int(Now) - 1) Then rng.Item(i).EntireRow.Delete
Next i
End Sub

I don't know how to include name with email address from Google either,
but signature line at bottom works. Thanks.
---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

mchen said:
Hi David,

Thanks for all your help. I'm sorry, my name is Michelle, I don't
know how to use my name to post my messages.
I have another problem, wondering if you can help me.
I have an excel sheet that has cumulative data. I would like the
macro to clean up the sheet by searching on column B and delete all
the rows that do not have previous day's data.
So today is May 17th, I would like the macro to keep all of May 16th's
data. The date format is in M/D/YYYY.

Thanks again,
Michelle

David McRitchie said:
Hi "mchen" [email protected],

Delete rows with "N" in Column 31 (col AE)
from my http://www.mvps.org/dmcritchie/excel/delempty.htm

Sub Delete_N_MarkedRows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim lastrow As Long, r As Long
lastrow = ActiveSheet.UsedRange.Rows.Count
For r = lastrow To 1 Step -1
If UCase(Cells(r, 31).Value) = "N" Then Rows(r).Delete
Next r
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

If the string can occur in any column you might
experiment with vba FIND

Here is a posting by Patric Molloy:
http://google.com/groups?selm=uhvafZVlCHA.216@tkmsftngp07

I would suggest using your name when posting, it is a lot more friendly
---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

Hi,

I have an excel file that always come with a line at the end with
"downloaded:" in the cell. How can I write a macro to search for that
cell and delete the row that cell is in?

Thanks for all your help!
 
D

David McRitchie

Hi Michelle,
A lot of what the code does is explained in
http://www.mvps.org/dmcritchie/excel/proper.htm
though I did not put in code to make it run faster.


Sub the beginning of a macro, the name of the
macro follows. A parameter list follows that, and
there are no parameters.

Dim is short for dimension and is used to declare
variables. The variable Rng is a Range of cells.
The variable I is an integer (32 bits as opposed to 16 bits).

Dates are measured in days past a certain date (Dec 31, 1899
for the 1900 date system), and time is measured as fractions of
a day. So INT(NOW) removes the fractional part of the
datetimestamp obtained from NOW.I. .

Set rng = Intersect(Range("A:A"), _
ActiveSheet.UsedRange.SpecialCells(xlConstants, xlNumbers))

the underscore (space underscore) indicates that the next line
is a continuation of the current line. Intersect is taking two ranges
and the resulting range (does not have to be contiguous) is comprised
of cells that exist in both of the ranges. So a cell must be in column AS
and it must be a constant with a number (no formulas, text, space or empty
cells). SpecialCells in itself is limited to the used range.

For i = rng.Count To 1 Step -1

This is a loop and for inserting/deleting rows we want to start from
the bottom and work our way up so we don't skip over rows.
Rng.Count is a count of items in a collection of cells named Rng.
The Step -1 is a negative increment (we are going from bottom to top)
so it starts out high and decremnts to 1.

rng.item(i) is a specific item in the range Rng specicall the i-th item.
The .value indicates we are checking the value of the item not
..text which woujld be the displayed or formatted value, not address which
would be the cells address, nor a heck of a lot of other choices.

(Int(Now) - 1) is yesterday's date starting at midnight which is considered
on computers to be part of the same day as one minute after midnight.
as a time of 00:00 zero hours, zero minutes. Anything before that
would be considered two days old -- going by days not by time.

EntireRow is used to mean the entiere row as opposed to the
default which would be one cell, if deleting.

Next is the end of the loop and returns to the the For statement for
an increment of test to see if it is finished; otherwise, continues one
more iteration of hte loop, which will be retested again and again, til done.

End Sub the end of the macro.


Don't know if that helps, but if you have no experience with
any programming language you might be better off purchasing a book..
I would look over some VBA tutorials first so you actually purchase
a book you can use as a reference instead of something too elementary.
http://www.mvps.org/dmcritchie/excel/excel.htm#tutorials

Another approach would be to just use the macros until you find they
don't do what you want, as soon as you change one line -- you are
a programmer (oops, I think that is developer now).

The macro previous to this one had Ucase which is upper case
Basically things in in VBA are case sensitive and things in
Worksheet functions are case insentive. There are exceptions in
both. http://www.mvps.org/dmcritchie/excel/string.htm#sensitivity
 
Top