|
-
Jun 27th, 2001, 09:55 AM
#1
Thread Starter
Fanatic Member
VBA Question on Date's in Excel
Is it possible to write a code that will look at a range of cells that have dates, and if the dates are 60 days old they will be highlighted like that in the conditional formating.
I am currently looking through some code I have that work with dates, I have not come across something like this...
If there are any ideas out there for me I would be appreciative..
Thank You..
-
Jun 27th, 2001, 10:30 AM
#2
You could take a look at the DATEDIFF() function, though I've never used this myself.
This should do the trick though :
Code:
If CDate(Excel.Applicaton.Range("A1").value) > (Date() + 60) then ...
and I've even tested that one - 1st time this year
-
Jun 27th, 2001, 11:06 AM
#3
Thread Starter
Fanatic Member
It is the first time for me as well...
I tried your suggestion, and nothing happens..
I don't know If I have to refer to the worksheet in the code.
As it is now this is what my code looks like. I am not getting any error when I run it, but the formating is not working.
Sub DateCheck ()
If CDate(Range("B7").Value) = (Date + 26) Then Range("p7").Value = "Yes"
End Sub
There has to be something I am forgetting?????
-
Jun 27th, 2001, 01:01 PM
#4
Lively Member
Try this one
Code:
Sub CheckDate()
Dim CellDate As Date
CellDate = CDate(Range("A1").Value)
If Date - CellDate > 60 Then Range("A1").Font.Bold = True
End Sub
-
Jun 27th, 2001, 01:32 PM
#5
Thread Starter
Fanatic Member
That worked for the one cell range. What I need is for that to scan a range of cells such as :
Dim CellDate As Date
CellDate = CDate(Range("B7:B9").Value)
If Date - CellDate > 10 Then Range("B7:B9").Font.Bold = True
When I run this code I get a Type Mismatch Error!!
Also when i try to change the format of the cell to highlight in yelllow when the logic statement is true, I get an error message say that it can not change the Color INdex.
Any guesses as to how to solve these two problems??
Thanks for the help so far...
-
Jun 27th, 2001, 02:14 PM
#6
Lively Member
If you have multiple cells then you would need to create a loop around these cells to check each one individually..
Lets try this instead
VB Code:
Sub DateCheck()
Dim CellDates(), i As Integer, j As Integer
'Assuming Cell A1 is the start of this range of dates and we do not know
'the last cell but the cell after the last cell is empty
Range("A1").Select
While Activecell.Value <> ""
i = i + 1
Activecell.Offset(1,0).Select
Wend
'Now redim the CellDates Array so we can populate it with all values
ReDim CellDates(1 to i)
'place each cell into the array
Range("A1").Select
For j = 1 to i
CellDates(j) = "A" & j
Next j
'Now loop through this array checking the values of the cell and changing the font to bold
'and the cell background to yellow
For j = 1 to i
If Date - Cdate(Range(Celldates(j)).Value) > 60 then
Range(CellDates(j)).Font.Bold = True
Range(CellDates(j)).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
Next j
End Sub
That should do the trick
-
Jun 27th, 2001, 02:32 PM
#7
Thread Starter
Fanatic Member
It looks like this is going to work. the only thing is when I tried the code, I received a Type-mismatch error on this line of code:
If Date - CDate(Range(CellDates(j)).Value) > 60 Then
My first cell starts on B7, so I inputted that instead of A1.
An idea why the error??
-
Jun 27th, 2001, 02:35 PM
#8
Lively Member
The value in the cell is invalid... put the code in debug mode and step through until you reach the error..
What is the cell's value??
-
Jun 27th, 2001, 02:41 PM
#9
Thread Starter
Fanatic Member
The cell value is 'DATE'....
When I returned to the worksheet after compiling the code, I noticed that cells A1:A5 were higlighted in yellow with a bold font.
I thought that if I changed the code to read the first cell as B7 that it would loop from there????
-
Jun 27th, 2001, 02:45 PM
#10
Thread Starter
Fanatic Member
The cell value that i gave was referring to the Column heading. It was reading from cell B6 which has the value DATE.
-
Jun 27th, 2001, 02:46 PM
#11
try a for..next loop with Cells() instead
A1 is 1,1
B7 is 7,2
Cells(row,column)
Sub CheckDate()
Dim CellDate As Date
for x = 7 to 9
CellDate = CDate(Cells(x,2).Value)
If Date - CellDate > 60 Then cells(2,x).Font.Bold = True
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 27th, 2001, 02:52 PM
#12
Lively Member
Not unless you change this aswell
VB Code:
For j = 1 to i
CellDates(j) = "A" & j
Next j
to this
VB Code:
For j = 1 to i
CellDates(j) = "B" & j + 6
Next j
If you are picking up a value in a cell that does not equal a date value then you will need to error trap like so
VB Code:
For j = 1 to i
On Error Goto InvalDate
If Date - Cdate(Range(Celldates(j)).Value) > 60 then
Range(CellDates(j)).Font.Bold = True
Range(CellDates(j)).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
On Error Goto 0
Next j
Exit Sub
InvalDate:
msgbox Err.Description
Exit Sub
End Sub
I've just tried my code on a full column of random dates and and it work no problems
-
Jun 27th, 2001, 03:09 PM
#13
Thread Starter
Fanatic Member
Now I am receiving a Subscript Out of Range error. When I run the debugger it brings up this line of code in yellow:
ReDim CellDates(1 To i)
-
Jun 27th, 2001, 03:13 PM
#14
Lively Member
I'll try a re-create your spreadsheet and see what I can change in the code for you
-
Jun 27th, 2001, 03:18 PM
#15
Lively Member
Hmmm..
I changed my Code to this
VB Code:
Sub DateCheck()
Dim CellDates(), i As Integer, j As Integer
'Assuming Cell A1 is the start of this range of dates and we do not know
'the last cell but the cell after the last cell is empty
Range("B7").Select
While ActiveCell.Value <> ""
i = i + 1
ActiveCell.Offset(1, 0).Select
Wend
'Now redim the CellDates Array so we can populate it with all values
ReDim CellDates(1 To i)
'place each cell into the array
Range("B7").Select
For j = 1 To i
CellDates(j) = "B" & j + 6
Next j
'Now loop through this array checking the values of the cell and changing the font to bold
'and the cell background to yellow
For j = 1 To i
If Date - CDate(Range(CellDates(j)).Value) > 60 Then
Range(CellDates(j)).Font.Bold = True
Range(CellDates(j)).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
Next j
End Sub
And it worked.. the spreadsheet that I am using has only 4 populated cells B6 to B9 with B6 Containing "DATE" and the remaining three cells contain the dates 1/1/01, 31/3/01, 1/6/01 respectively
-
Jun 27th, 2001, 03:21 PM
#16
Thread Starter
Fanatic Member
Thank You DJDANNYK...your code did work, i forgot to chane the range address to B7.
You have helped me immensely!!!!!!!!!
I have also learned alot from you in this experience....
Keep up the great work!!!!!!!!!!
-
Jun 27th, 2001, 03:25 PM
#17
Lively Member
No problem..
I prefer to put extra stuff into the code examples to help newbies learn additional stuff that they might not of thought of..
Come back to me if you have any other questions with VBA for Excel... been using VBA for Excel for 3 years now, and I'm considered THE Guru around my workplace
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|