PC Review


Reply
Thread Tools Rate Thread

delete all rows where cell is not date format

 
 
burl_h
Guest
Posts: n/a
 
      11th Feb 2008
I'm having a problem with the following code, for some reason I can't
get the correct syntax.

Sub delete_not_date
Dim datarng As Range
lastrow = Cells(rows.Count, "B").End(xlUp).Row
Set datarng = Range("b2:b" & lastrow)
For i = lastrow To 1 Step -1
For Each cell In datarng
If Not cell.Format = "mm/dd/yyyy" Then
cell.EntireRow.Delete
End If
Next cell
Next i
End Sub

My problem is the date format, I tried many variations but can't get
anything to work.

I did check the format on the cells and found them to be a custom
format written as "mm\/mm\/yyyy". The data originated from a Crystal
report, hence perhaps the weird date format.

The objective is to delete all rows that don't contain a date value.

Thanks.
 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      11th Feb 2008
The cell property you need to check is NumberFormat, not just Format.

Rick


"burl_h" <(E-Mail Removed)> wrote in message
news:2de132ca-9fc8-4e4d-956b-(E-Mail Removed)...
> I'm having a problem with the following code, for some reason I can't
> get the correct syntax.
>
> Sub delete_not_date
> Dim datarng As Range
> lastrow = Cells(rows.Count, "B").End(xlUp).Row
> Set datarng = Range("b2:b" & lastrow)
> For i = lastrow To 1 Step -1
> For Each cell In datarng
> If Not cell.Format = "mm/dd/yyyy" Then
> cell.EntireRow.Delete
> End If
> Next cell
> Next i
> End Sub
>
> My problem is the date format, I tried many variations but can't get
> anything to work.
>
> I did check the format on the cells and found them to be a custom
> format written as "mm\/mm\/yyyy". The data originated from a Crystal
> report, hence perhaps the weird date format.
>
> The objective is to delete all rows that don't contain a date value.
>
> Thanks.


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Feb 2008
Along with the comment that Rick made, your custom format of mm/\mm/\yyyy looks
really weird.

Was that a mistake in the post?

And you don't want to loop within your loop.

I'd use:

Option Explicit
Sub delete_non_dates()
dim iRow as long
Dim FirstRow as long
dim LastRow as long
with activesheet
firstrow = 2 'or 1?????
lastrow = .Cells(.rows.Count, "B").End(xlUp).Row
for irow = lastrow to firstrow Step -1
If .cells(irow,"B").numberformat = "mm/dd/yyyy" Then
'keep it
else
.rows(irow).delete
end if
next irow
end with
End Sub



burl_h wrote:
>
> I'm having a problem with the following code, for some reason I can't
> get the correct syntax.
>
> Sub delete_not_date
> Dim datarng As Range
> lastrow = Cells(rows.Count, "B").End(xlUp).Row
> Set datarng = Range("b2:b" & lastrow)
> For i = lastrow To 1 Step -1
> For Each cell In datarng
> If Not cell.Format = "mm/dd/yyyy" Then
> cell.EntireRow.Delete
> End If
> Next cell
> Next i
> End Sub
>
> My problem is the date format, I tried many variations but can't get
> anything to work.
>
> I did check the format on the cells and found them to be a custom
> format written as "mm\/mm\/yyyy". The data originated from a Crystal
> report, hence perhaps the weird date format.
>
> The objective is to delete all rows that don't contain a date value.
>
> Thanks.


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Feb 2008
And just in case...

Change this line:
If .cells(irow,"B").numberformat = "mm/dd/yyyy" Then
to
If lcase(.cells(irow,"B").numberformat) = lcase("mm/dd/yyyy") Then



Dave Peterson wrote:
>
> Along with the comment that Rick made, your custom format of mm/\mm/\yyyy looks
> really weird.
>
> Was that a mistake in the post?
>
> And you don't want to loop within your loop.
>
> I'd use:
>
> Option Explicit
> Sub delete_non_dates()
> dim iRow as long
> Dim FirstRow as long
> dim LastRow as long
> with activesheet
> firstrow = 2 'or 1?????
> lastrow = .Cells(.rows.Count, "B").End(xlUp).Row
> for irow = lastrow to firstrow Step -1
> If .cells(irow,"B").numberformat = "mm/dd/yyyy" Then
> 'keep it
> else
> .rows(irow).delete
> end if
> next irow
> end with
> End Sub
>
> burl_h wrote:
> >
> > I'm having a problem with the following code, for some reason I can't
> > get the correct syntax.
> >
> > Sub delete_not_date
> > Dim datarng As Range
> > lastrow = Cells(rows.Count, "B").End(xlUp).Row
> > Set datarng = Range("b2:b" & lastrow)
> > For i = lastrow To 1 Step -1
> > For Each cell In datarng
> > If Not cell.Format = "mm/dd/yyyy" Then
> > cell.EntireRow.Delete
> > End If
> > Next cell
> > Next i
> > End Sub
> >
> > My problem is the date format, I tried many variations but can't get
> > anything to work.
> >
> > I did check the format on the cells and found them to be a custom
> > format written as "mm\/mm\/yyyy". The data originated from a Crystal
> > report, hence perhaps the weird date format.
> >
> > The objective is to delete all rows that don't contain a date value.
> >
> > Thanks.

>
> --
>
> Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
burl_h
Guest
Posts: n/a
 
      11th Feb 2008
On Feb 10, 8:06*pm, "Rick Rothstein \(MVP - VB\)"
<rick.newsNO.S...@NO.SPAMverizon.net> wrote:
> The cell property you need to check is NumberFormat, not just Format.
>
> Rick
>
> "burl_h" <milli...@zoominternet.net> wrote in message
>
> news:2de132ca-9fc8-4e4d-956b-(E-Mail Removed)...
>
>
>
> > I'm having a problem with the following code, for some reason I can't
> > get the correct syntax.

>
> > Sub delete_not_date
> > Dim datarng As Range
> > lastrow = Cells(rows.Count, "B").End(xlUp).Row
> > Set datarng = Range("b2:b" & lastrow)
> > * * *For i = lastrow To 1 Step -1
> > * * * * *For Each cell In datarng
> > * * * * * * *If Not cell.Format = "mm/dd/yyyy" Then
> > * * * * * * * * *cell.EntireRow.Delete
> > * * * * * * *End If
> > * * * * *Next cell
> > * * *Next i
> > End Sub

>
> > My problem is the date format, I tried many variations but can't get
> > anything to work.

>
> > I did check the format on the cells and found them to be a custom
> > format written as "mm\/mm\/yyyy". The data originated from a Crystal
> > report, hence perhaps the weird date format.

>
> > The objective is to delete all rows that don't contain a date value.

>
> > Thanks.- Hide quoted text -

>
> - Show quoted text -


Rick,

Thanks for your solution, it worked fine. I did however have to change
the format to "mm\/dd\/yyyy" to make it work, the little quirk with
Crystal reports no doubt.

Thanks
burl_h
 
Reply With Quote
 
burl_h
Guest
Posts: n/a
 
      11th Feb 2008
On Feb 10, 8:33*pm, burl_h <milli...@zoominternet.net> wrote:
> On Feb 10, 8:06*pm, "Rick Rothstein \(MVP - VB\)"
>
>
>
>
>
> <rick.newsNO.S...@NO.SPAMverizon.net> wrote:
> > The cell property you need to check is NumberFormat, not just Format.

>
> > Rick

>
> > "burl_h" <milli...@zoominternet.net> wrote in message

>
> >news:2de132ca-9fc8-4e4d-956b-(E-Mail Removed)...

>
> > > I'm having a problem with the following code, for some reason I can't
> > > get the correct syntax.

>
> > > Sub delete_not_date
> > > Dim datarng As Range
> > > lastrow = Cells(rows.Count, "B").End(xlUp).Row
> > > Set datarng = Range("b2:b" & lastrow)
> > > * * *For i = lastrow To 1 Step -1
> > > * * * * *For Each cell In datarng
> > > * * * * * * *If Not cell.Format = "mm/dd/yyyy" Then
> > > * * * * * * * * *cell.EntireRow.Delete
> > > * * * * * * *End If
> > > * * * * *Next cell
> > > * * *Next i
> > > End Sub

>
> > > My problem is the date format, I tried many variations but can't get
> > > anything to work.

>
> > > I did check the format on the cells and found them to be a custom
> > > format written as "mm\/mm\/yyyy". The data originated from a Crystal
> > > report, hence perhaps the weird date format.

>
> > > The objective is to delete all rows that don't contain a date value.

>
> > > Thanks.- Hide quoted text -

>
> > - Show quoted text -

>
> Rick,
>
> Thanks for your solution, it worked fine. I did however have to change
> the format to "mm\/dd\/yyyy" to make it work, the little quirk with
> Crystal reports no doubt.
>
> Thanks
> burl_h- Hide quoted text -
>
> - Show quoted text -


Dave,

Yes that was a typo on my part "mm\/mm\/yyyy" should be "mm\/dd\/yyyy"

I did try your solution and it worked great, thanks very much for
everyone's help.

Thanks
burl_h
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
if cell is not date format delete row burl_h Microsoft Excel Programming 1 11th Feb 2008 09:47 AM
if cell is not date format delete row burl_h Microsoft Excel Programming 0 11th Feb 2008 12:27 AM
if cell is not date format delete row burl_h Microsoft Excel Programming 0 11th Feb 2008 12:27 AM
How do you format an auto update to delete rows with expired date =?Utf-8?B?Um5lZWRzaGVscA==?= Microsoft Excel Worksheet Functions 0 29th May 2007 04:30 PM
macro to find date format in a cell and delete that entire row vikram Microsoft Excel Misc 8 30th Apr 2004 05:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:55 PM.