harnden2005
New Member
- Joined
- May 26, 2009
- Messages
- 3
Hi, I'm having a real problem trying to delete entire rows above and below a certain range using VBA macro.
I have a userform made up which allows the user to select the From (mm/yyyy) and To (mm/yyyy) dates. So that the user can pull out a wanted range from the raw data. the data is contained within A1:C300 range. Where the first coloumn is Date i.e. 07/1999.
So what i was thinking is to write a loop that compares the dates to the dates entered by the user in the userform and if the date less than From Date delete all the rows above that date and if the date is greater than the To date entered by the user it should delete all the rows below that date. I wrote a code but it only works for the From date and when I use the same code for the To date it deletes everything.
here is my code that I got from your website:
Sub DateFormat(Data)
'Compares date with the date that the user input and gives out a range of exchange rates_
'beginning with that date
Dim LR As Date
LR = Range("a" & Rows.count).End(xlUp).row
For r = LR To 4 Step -1
If Cells(r, "a").Value < Data Then Rows(r).Delete
Next r
End Sub
Sub DateFormat1(Data)
Dim LR As Date
LR = Range("a" & Rows.count).End(xlUp).row
For r = LR To 4 Step -1
If Cells(r, "a").Value > Data1 Then Rows(r).Delete
Next r
End Sub
where
Dim Data As Date
Data = .ComboBox1 & "/" & .ComboBox2
Dim Data1 As Date
Data1 = .ComboBox3 & "/" & .ComboBox4
code is in userform command button
Any help would be much appreciated,
Thank you
I have a userform made up which allows the user to select the From (mm/yyyy) and To (mm/yyyy) dates. So that the user can pull out a wanted range from the raw data. the data is contained within A1:C300 range. Where the first coloumn is Date i.e. 07/1999.
So what i was thinking is to write a loop that compares the dates to the dates entered by the user in the userform and if the date less than From Date delete all the rows above that date and if the date is greater than the To date entered by the user it should delete all the rows below that date. I wrote a code but it only works for the From date and when I use the same code for the To date it deletes everything.
here is my code that I got from your website:
Sub DateFormat(Data)
'Compares date with the date that the user input and gives out a range of exchange rates_
'beginning with that date
Dim LR As Date
LR = Range("a" & Rows.count).End(xlUp).row
For r = LR To 4 Step -1
If Cells(r, "a").Value < Data Then Rows(r).Delete
Next r
End Sub
Sub DateFormat1(Data)
Dim LR As Date
LR = Range("a" & Rows.count).End(xlUp).row
For r = LR To 4 Step -1
If Cells(r, "a").Value > Data1 Then Rows(r).Delete
Next r
End Sub
where
Dim Data As Date
Data = .ComboBox1 & "/" & .ComboBox2
Dim Data1 As Date
Data1 = .ComboBox3 & "/" & .ComboBox4
code is in userform command button
Any help would be much appreciated,
Thank you