ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dates (https://www.excelbanter.com/excel-programming/381638-re-dates.html)

Martin Fishlock

Dates
 
Hi Janos:

You were nearly the

1. Dates need to be surrounded by the # sign.
2. You need to use and not '&'.
3. It is a good idea to use option explicit this ensures that you declare
all variables reduced errors from spelling misstakes/typos.
4. I moved the references to the worksheet and the range out so that it is
easier to maintain.
5. Date literials are quite risky if you work in a multi country enviroment.
It may be better to use dateserial but that can't be a constant (not sure
about 2007).

Here's the code:

Sub formatcells()
' use constants as they are easier to change later....
Const cws_name As String = "Instructions"
Const cws_range As String = "B7:B200"
Const limitDOWN As Date = #1/1/2007#
Const limitUP As Date = #2/2/2007#

Dim c As Range

For Each c In Worksheets(cws_name).Range(cws_range).Cells
If c limitDOWN And c < limitUP Then
With c.Font
.Bold = True
.Italic = True
End With
End If
Next c

End Sub



--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"Janos" wrote:

Hello,

Sorry for the silly/basic question, but how can I compare dates? And would I
be able to see if my cell is in a range of dates? I have this code that
doesn't work:

Const limitDOWN As Date = 1 / 1 / 2007
Const limitUP As Date = 2 / 2 / 2007
For Each c In Worksheets("Instructions").Range("B7:B200").Cells
If c.Date limitDOWN & c.Date < limitUPThen
With c.Font
.Bold = True
.Italic = True
End With
End If
Next c

Where is my Error?



All times are GMT +1. The time now is 02:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com