Hello
I have been writing a piece of code that (among other things) calculates the last day of the prior month, given as input the date for tomorrow.
As a background, I am using Excel 2003 SP3 (English language version), and my regional settings in Windows are Finnish, i.e. the standard date format is dd.mm.yyyy.
Assume that I have a worksheet with the date August 6, 2009 written into cell A1. This is written as an Excel date (numeric value 40030), which appears as 6.8.2009 with my regional settings.
I have written the following test code. Reference to the Analysis toolpak (atpvbaen.xls) is in place as it is supposed to be.
Option Explicit
Sub test()
Dim MaxDate As Date
MaxDate = Fix(Application.WorksheetFunction.Max(Range(Cells(1, 1), Cells(2, 1)))) + 1
MsgBox "Date is " & Format(eomonth(MaxDate, -1), "dd.mm.yyyy")
End Sub
Sub AddMenu()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Test").Delete
On Error GoTo 0
Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
iHelpMenu = cbMainMenuBar.Controls("Help").Index
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)
cbcCutomMenu.Caption = "Test"
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Perform test"
.OnAction = "test"
End With
End Sub
Sub DeleteMenu()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Test").Delete
On Error GoTo 0
End Sub
In addition, the following code resides in the ThisWorkbook section:
Private Sub Workbook_Activate()
Run "AddMenu"
End Sub
Private Sub Workbook_beforeclose(Cancel As Boolean)
Run "DeleteMenu"
End Sub
Problem:
When I launch the "test" routine from the Tools->Macro->Macros menu, it displays that "Date is 31.07.2009" as it is supposed to do. However, when I run the code from the "Test" menu, it displays that "Date is 30.06.2009". If I change the date value from August 6, 2009 to August 5, 2009 in the cell and rerun the code, it displays the correct July 31 value when started from the Macros menu, but now it displays "Date is 31.05.2009" when launched from the "Test" menu.
I suppose there is something messed up with the use of date formats here. It would appear that Excel is treating the date of August 6, 2009 as 6.8.2009 while interpreting this as JUNE 8, 2009 rather than August 6, 2009 which it *really* is. How could I fix this annoying behavior so that I would not have to ask my customers to launch the application from the "Macros" menu?
I have been writing a piece of code that (among other things) calculates the last day of the prior month, given as input the date for tomorrow.
As a background, I am using Excel 2003 SP3 (English language version), and my regional settings in Windows are Finnish, i.e. the standard date format is dd.mm.yyyy.
Assume that I have a worksheet with the date August 6, 2009 written into cell A1. This is written as an Excel date (numeric value 40030), which appears as 6.8.2009 with my regional settings.
I have written the following test code. Reference to the Analysis toolpak (atpvbaen.xls) is in place as it is supposed to be.
Option Explicit
Sub test()
Dim MaxDate As Date
MaxDate = Fix(Application.WorksheetFunction.Max(Range(Cells(1, 1), Cells(2, 1)))) + 1
MsgBox "Date is " & Format(eomonth(MaxDate, -1), "dd.mm.yyyy")
End Sub
Sub AddMenu()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Test").Delete
On Error GoTo 0
Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
iHelpMenu = cbMainMenuBar.Controls("Help").Index
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)
cbcCutomMenu.Caption = "Test"
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Perform test"
.OnAction = "test"
End With
End Sub
Sub DeleteMenu()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Test").Delete
On Error GoTo 0
End Sub
In addition, the following code resides in the ThisWorkbook section:
Private Sub Workbook_Activate()
Run "AddMenu"
End Sub
Private Sub Workbook_beforeclose(Cancel As Boolean)
Run "DeleteMenu"
End Sub
Problem:
When I launch the "test" routine from the Tools->Macro->Macros menu, it displays that "Date is 31.07.2009" as it is supposed to do. However, when I run the code from the "Test" menu, it displays that "Date is 30.06.2009". If I change the date value from August 6, 2009 to August 5, 2009 in the cell and rerun the code, it displays the correct July 31 value when started from the Macros menu, but now it displays "Date is 31.05.2009" when launched from the "Test" menu.
I suppose there is something messed up with the use of date formats here. It would appear that Excel is treating the date of August 6, 2009 as 6.8.2009 while interpreting this as JUNE 8, 2009 rather than August 6, 2009 which it *really* is. How could I fix this annoying behavior so that I would not have to ask my customers to launch the application from the "Macros" menu?