Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default first Tuesday in the month

I want to use a vba procedure to examine the dates in a calendar and
highlight all those that are the first Tuesday in the month.

Can this be done?



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default first Tuesday in the month

On Sat, 29 Nov 2003 23:26:26 +1100, "DL" wrote:

I want to use a vba procedure to examine the dates in a calendar and
highlight all those that are the first Tuesday in the month.

Can this be done?



Well, a formula which will compute the first Tuesday in any given month is:


=DATE(YEAR(A1),MONTH(A1),1)+CHOOSE(WEEKDAY(DATE(YE AR(A1),MONTH(A1),1)),2,1,0,6,5,4,3)

So you could use Conditional Formatting. You could certainly use VBA to apply
conditional formatting to the range in which your calendar exists.

In VBA code it might be something like:


calendar.FormatConditions.Delete
calendar.FormatConditions.Add Type:=xlExpression, Formula1:= _

"=A1=DATE(YEAR(A1),MONTH(A1),1)+CHOOSE(WEEKDAY(DAT E(YEAR(A1),MONTH(A1),1)),2,1,0,6,5,4,3)"
calendar.FormatConditions(1).Interior.ColorIndex = 15

where 'calendar' represents the range where the calendar is stored.






--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default first Tuesday in the month

Hi,

For any date in A1, the following formula returns TRUE if it's first Tuesday of
the month.

=A1=A1-DAY(A1)+8-WEEKDAY(A1-DAY(A1)+5)

So, via direct formatting :
Public Sub TestHighLight()
HighLight1stTuesdayOfMonth Range("A1:A300") ' or whatever
End Sub

Public Sub HighLight1stTuesdayOfMonth(Rng As Range)
Dim C As Range
For Each C In Rng
If IsDate(C) Then
If C = C - Day(C) + 8 - Weekday(C - Day(C) + 5) Then
C.Interior.ColorIndex = 3
End If
End If
Next C
End Sub

Or via conditional formatting (preferred way):

Sub PutCondFormatviaVBA()
With Range("A1:A300") ' or whatever
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=A1=A1-DAY(A1)+8-WEEKDAY(A1-DAY(A1)+5)"
.FormatConditions(1).Interior.ColorIndex = 3
End With
End Sub

Regards,

Daniel M.

"DL" wrote in message
...
I want to use a vba procedure to examine the dates in a calendar and
highlight all those that are the first Tuesday in the month.

Can this be done?





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default first Tuesday in the month

Formula can be abbreviated to :

=DAY(A1)+WEEKDAY(A1-DAY(A1)+5)=8

So this new formula can replace the old one in PutCondFormatviaVBA()


Accordingly, in the HighLight1stTuesdayOfMonth(),
Replace:
If C = C - Day(C) + 8 - Weekday(C - Day(C) + 5) Then
By:
If Day(C) + Weekday(C - Day(C) + 5) = 8 Then

Regards,

Daniel M.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default first Tuesday in the month

Daniel,
Sub PutCondFormatviaVBA.
That's a fabulous little macro. Thanks a million.
--
Regards,
David Lindstrom


"Daniel.M" wrote in message
...
Hi,

For any date in A1, the following formula returns TRUE if it's first

Tuesday of
the month.

=A1=A1-DAY(A1)+8-WEEKDAY(A1-DAY(A1)+5)

So, via direct formatting :
Public Sub TestHighLight()
HighLight1stTuesdayOfMonth Range("A1:A300") ' or whatever
End Sub

Public Sub HighLight1stTuesdayOfMonth(Rng As Range)
Dim C As Range
For Each C In Rng
If IsDate(C) Then
If C = C - Day(C) + 8 - Weekday(C - Day(C) + 5) Then
C.Interior.ColorIndex = 3
End If
End If
Next C
End Sub

Or via conditional formatting (preferred way):

Sub PutCondFormatviaVBA()
With Range("A1:A300") ' or whatever
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=A1=A1-DAY(A1)+8-WEEKDAY(A1-DAY(A1)+5)"
.FormatConditions(1).Interior.ColorIndex = 3
End With
End Sub

Regards,

Daniel M.

"DL" wrote in message
...
I want to use a vba procedure to examine the dates in a calendar and
highlight all those that are the first Tuesday in the month.

Can this be done?







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tuesday JDay Excel Discussion (Misc queries) 1 February 3rd 10 09:58 PM
Formula to Return the next 1st Tuesday of a Month Sean Excel Worksheet Functions 42 December 3rd 07 08:22 AM
dates and 1st Tuesday of month Jessica[_2_] Excel Worksheet Functions 11 July 17th 07 11:13 PM
Every 1st, 3rd, 5th Tuesday Rick[_3_] Excel Worksheet Functions 9 June 12th 07 02:05 PM
Calculating first/last Monday, Tuesday, etc. in a given month in E Rossta Excel Worksheet Functions 3 May 27th 06 02:19 AM


All times are GMT +1. The time now is 11:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"