Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Delete rows within a date range

Hi I'm new to vba programming, here is my problem. I have 2 variable
one named start_date and the other end_date. What I am trying to do i
go through a worksheet and look at a cell in each row called startdat
and compare it to the dates that were entered into the variables above
If the date in the cell in each row is not = to the start_dat
variable and is not <= the end_date variable then I want to delete tha
entire row. Basically if the date in the cell is not within the dat
range of the two variables, then that row needs to be deleted.

It needs to go through the whole worksheet to eliminate all rows wit
dates that don't fall within this range.

Can someone please help I am desperate.
Thank

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Excel VBA - Delete rows within a date range

rott,

If your dates are in column A, then you can use the macro below. If the
dates are in another column, change all instances of [1] to correspond to
the actual column # - if the dates are in column H, for example, use [8].
You can either change the start and end dates in my code, or use global
variables to pass the dates in (in which case delete the Dim
statements......)

HTH,
Bernie
MS Excel MVP

Sub KeepDates()
Dim myRows As Long
Dim Start_Date As Date
Dim End_Date As Date
Start_Date = "1/2/4"
End_Date = "1/7/4"

Range("A1").EntireColumn.Insert
Range("A1").FormulaR1C1 = _
"=IF(ISNUMBER(RC[1]),IF(AND(RC[1]=DATEVALUE(""1/2/4"")," & _
"RC[1]<=DATEVALUE(""1/7/4"")), ""Keep"",""Trash""),""Keep"")"
myRows = ActiveSheet.UsedRange.Rows.Count
Range("A1").Copy Range("A1:A" & myRows)
With Range(Range("A1"), Range("A1").End(xlDown))
.Copy
.PasteSpecial Paste:=xlValues
End With
Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending
Columns("A:A").Find(What:="Trash", After:=Range("A1")).Select
Range(Selection, Selection.End(xlDown)).EntireRow.Delete
Range("A1").EntireColumn.Delete

End Sub

"rott " wrote in message
...
Hi I'm new to vba programming, here is my problem. I have 2 variables
one named start_date and the other end_date. What I am trying to do is
go through a worksheet and look at a cell in each row called startdate
and compare it to the dates that were entered into the variables above.
If the date in the cell in each row is not = to the start_date
variable and is not <= the end_date variable then I want to delete that
entire row. Basically if the date in the cell is not within the date
range of the two variables, then that row needs to be deleted.

It needs to go through the whole worksheet to eliminate all rows with
dates that don't fall within this range.

Can someone please help I am desperate.
Thanks


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Delete rows within a date range

Hey thanks man I will try that out, but i got one question the dates ar
all in column G So by your macro that would be number 7 that i change.
My question is do i change all of the A's used in the code to G's fo
column G or do i just change all the numbers.


thanks again

rot

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Excel VBA - Delete rows within a date range

Rott,

Just the numbers: a new column A is inserted and used for a "flagging"
formula, as is then deleted after use.

Bernie
MS Excel MVP

"rott " wrote in message
...
Hey thanks man I will try that out, but i got one question the dates are
all in column G So by your macro that would be number 7 that i change.
My question is do i change all of the A's used in the code to G's for
column G or do i just change all the numbers.


thanks again

rott


---
Message posted from http://www.ExcelForum.com/



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Delete rows within a date range

Hey Bernie sorry to bug you again but when i put this code into my macro
it always stops at this line:

Columns("A:A").Find(What:="Trash", After:=Range("A7")).Select

and gives this error message:

Runtime error 91 - Object Variable or withblock variable not set.

The only thing I can think of is the portion that says "What" right
after Find. Is this supposed to be defined somewhere?

I really appreciate the help. If I can get this working it would be
fantastic.

thanks
rott


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Excel VBA - Delete rows within a date range

rott,

Sorry for not being clear, but when I said change all instances of [1] to
[7] I meant only those instances of 1s inside square brackets. Copy the
code below and give it a try. I have made the changes as needed, so don't
change anything.

HTH,
Bernie
MS Excel MVP

Sub KeepDates()
Dim myRows As Long
Dim Start_Date As Date
Dim End_Date As Date
Start_Date = "1/2/4"
End_Date = "1/7/4"

Range("A1").EntireColumn.Insert
Range("A1").FormulaR1C1 = _
"=IF(ISNUMBER(RC[7]),IF(AND(RC[7]=DATEVALUE(""1/2/4"")," & _
"RC[7]<=DATEVALUE(""1/7/4"")), ""Keep"",""Trash""),""Keep"")"
myRows = ActiveSheet.UsedRange.Rows.Count
Range("A1").Copy Range("A1:A" & myRows)
With Range(Range("A1"), Range("A1").End(xlDown))
.Copy
.PasteSpecial Paste:=xlValues
End With
Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending
Columns("A:A").Find(What:="Trash", After:=Range("A1")).Select
Range(Selection, Selection.End(xlDown)).EntireRow.Delete
Range("A1").EntireColumn.Delete

End Sub


"rott " wrote in message
...
Hey Bernie sorry to bug you again but when i put this code into my macro
it always stops at this line:

Columns("A:A").Find(What:="Trash", After:=Range("A7")).Select

and gives this error message:

Runtime error 91 - Object Variable or withblock variable not set.

The only thing I can think of is the portion that says "What" right
after Find. Is this supposed to be defined somewhere?

I really appreciate the help. If I can get this working it would be
fantastic.

thanks
rott


---
Message posted from http://www.ExcelForum.com/



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Delete rows within a date range

Hey thanks man it works now. I just have to change it to be able to ge
the date from the variables instead of having the date hard coded in.

The start_date and End_date variables are pulling the dates from a cel
on another worksheet. So it has to work with those dates

Would this work if I replaced this:

DATEVALUE(""1/7/4"")

with the variable for the start date instead like this

DATEVALUE(Start_date)

and then do the same for the end_date variable. Or is there somethin
else i need to do to make that change

--
Message posted from http://www.ExcelForum.com

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Excel VBA - Delete rows within a date range

rott,

Try this, below.

HTH,
Bernie
MS Excel MVP

Sub KeepDates()
Dim myRows As Long
Dim Start_Date As Date
Dim End_Date As Date
'Change the sheet and range address as appropriate
Start_Date = Worksheets("Sheet1").Range("D4").Value
End_Date = Worksheets("Sheet1").Range("D5").Value

Range("A1").EntireColumn.Insert
Range("A1").FormulaR1C1 = _
"=IF(ISNUMBER(RC[7]),IF(AND(RC[7]=DATEVALUE(""" _
& Start_Date & """)," & _
"RC[7]<=DATEVALUE(""" & End_Date & _
""")), ""Keep"",""Trash""),""Keep"")"
myRows = ActiveSheet.UsedRange.Rows.Count
Range("A1").Copy Range("A1:A" & myRows)
With Range(Range("A1"), Range("A1").End(xlDown))
.Copy
.PasteSpecial Paste:=xlValues
End With
Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending
Columns("A:A").Find(What:="Trash", After:=Range("A1")).Select
Range(Selection, Selection.End(xlDown)).EntireRow.Delete
Range("A1").EntireColumn.Delete

End Sub

"rott " wrote in message
...
Hey thanks man it works now. I just have to change it to be able to get
the date from the variables instead of having the date hard coded in.

The start_date and End_date variables are pulling the dates from a cell
on another worksheet. So it has to work with those dates

Would this work if I replaced this:

DATEVALUE(""1/7/4"")

with the variable for the start date instead like this

DATEVALUE(Start_date)

and then do the same for the end_date variable. Or is there something
else i need to do to make that change?


---
Message posted from http://www.ExcelForum.com/



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Delete rows within a date range

Well i tried that and it work great thanks again.

I have another question. I want to adapt the code to delete rows tha
have the text "No Impact" in column 12 (Column L). This is what
have so far but i get the same error as before.

Can you take a quick peek and tell me where i am going wrong. Thank
again man this is such a great help.

--------------------------------------------
Range("A1").EntireColumn.Insert
Range("A1").FormulaR1C1 = _
"=IF(RC[12])=""No Impact""" _
& """), ""Keep"",""Trash""),""Keep"")"
myRows = ActiveSheet.UsedRange.Rows.Count
Range("A1").Copy Range("A1:A" & myRows)
With Range(Range("A1"), Range("A1").End(xlDown))
.Copy
.PasteSpecial Paste:=xlValues
End With
Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending
Columns("A:A").Find(What:="Trash", After:=Range("A1")).Select
Range(Selection, Selection.End(xlDown)).EntireRow.Delete
-------------------------------------------------

--
Message posted from http://www.ExcelForum.com

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If Count is not in a certain range, delete Rows CarolynRatcliffe Excel Discussion (Misc queries) 0 February 23rd 10 04:52 AM
Select a range of rows to delete Donna[_3_] Excel Worksheet Functions 2 January 26th 10 07:32 PM
Delete rows based upon a range of times farmboy Excel Discussion (Misc queries) 6 October 16th 09 05:02 PM
Macro to delete blank rows in a data range Youlan Excel Discussion (Misc queries) 5 September 17th 08 08:51 AM
delete rows in range - macro hindu cliparts Excel Worksheet Functions 0 November 16th 06 09:54 PM


All times are GMT +1. The time now is 05:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"