PDA

View Full Version : identify dates in a cell using macro



Richard Smit
04-23-2008, 03:46 AM
Hello There,

I'm new to VBA in excel, I would like to identify the values in a range.

Let me explain in detail...

I have a column filled with various values,I need to copy the cells where the date is a future date, any date that is greater than the current date to a different workbook. I've managed to find out the way to copy date to different workbooks. However... I'm stuck when I have to identify if the date is a future date. Please help guys, I'm in urgent need of your help. Thanks.

- Richard

Bob Phillips
04-23-2008, 03:48 AM
Is there some difference between a future date and a date greater than the current date?



If Range("A1").Value > Date Then
'do something
End If


Obviously you will need to incorporate intou your code.

Richard Smit
04-23-2008, 08:50 PM
Thanks for the solution, However... Im still having issues.
Can you give a sample sheet that I can look at.

The Macro will copy and paste data to the next sheet if the value in Column A is a future date. Else it will do nothing.

Thanks,

Regards,
Richard

Oorang
04-23-2008, 08:56 PM
Try this:
Option Explicit

Sub CopyDate()
Dim cll As Excel.Range
Dim rngDates As Excel.Range
Dim wsOutput As Excel.Worksheet
Dim dtVal As Date
Dim dtNow As Date
Dim lngRow As Long
Set rngDates = Excel.Selection
Set rngDates = Excel.Intersect(rngDates.Parent.UsedRange, rngDates)
Set wsOutput = Excel.Workbooks.Add.Worksheets(1)
dtNow = Date
For Each cll In rngDates.Cells
If IsDate(cll.Value) Then
dtVal = CDate(cll.Value)
If dtVal > dtNow Then
lngRow = lngRow + 1
wsOutput.Cells(lngRow, 1).Value = dtVal
End If
End If
Next
End Sub