Domanda

I am trying to convert column with GMT hour to the specified time zones from the user.

I get an error when VBA attempts to subtract 18000 secs (GMT-5) from 01:00.

Selected_GMT = -18000
CellValue = "1/0/00 01:00"

New_Time = DateAdd("s", Selected_GMT,CellValue)

Is this error happening because VBA is unable to determine the hours before 00:00? I have figured out the seconds for Selected_GMT, how can I use that to determine New_Time?

È stato utile?

Soluzione

As ooo noted in a comment above, 1/0/00 is an invalid date code. However even if that was a typo in your question, the fact that the date uses a 2 digit year code begs the question "WHICH year 00?" Apologies if you already know this, but below I've extracted a recap of how Excel dates work from something that I've written elsewhere. The relevant part is "Day Zero And Before In Excel"; if the "00" actually represents *19*00 in the cell (as it will if you've just punched in "01:00 as the cell entry), you're going to run into problems subtracting from that. In which case, perhaps explicitly enter the date and time (perhaps using the current date) but hide the date component using formatting):

Excel uses a "date serial" system in which any date that you use in calculations is represented as a positive integer value. That integer value is calculated from an arbitrary starting date. Adding whole numbers to a specific serial date moves you forward through the calendar a day at a time, and subtracting whole numbers moves you backwards... as long as you don't go past the starting date of the serial number system and end up with a negative value. Times are represented as fractions of a day; 0.25 for 6am, 0.5 for noon, 0.75 for 6pm and so on.

Excel Dates

In the case of Excel for Windows, the starting date is 1 January 1900. That is, if you enter the value 1 into a cell in Excel and format it as a date, you'll see the value as 1 January 1900. 2 will be the 2nd of January 1900, 3 the 3rd of January, and so on. 367 represents 1 January 1901 because Excel treats 1900 as having been a leap year with 366 days. In other words, every full day that passes adds 1 to the serial date.

It's important to remember that the above relates to Excel only, and not to Access, SQL Server or other database products (or Visual Basic, for that matter). In Access, for example, the range of valid dates is 1 January 100 to 31 December 9999, the same range that can be stored in a VB or VBA variable with a Date data type.

Excel And The Macintosh

Macintosh systems use a start date of 1 January 1904, neatly bypassing the 1900 leap year issue. However that does mean that there's a 4 year discrepancy between the serial date values in a workbook created in Excel for Windows, and one created in Excel for the Mac. Fortunately under Tools -> Options-> Calculation (on pre-2007 versions of Excel) you'll find a workbook option called 1904 Date System. If that's checked, Excel knows that the workbook came from a Macintosh and will adjust its date calculations accordingly.

Excel Times

As noted in the introduction, times are calculated as a fraction of a day. For example 1.5 represents noon on 2 January 1900. 1.75 represents 6pm on 2 January 1900.

(Snipped a bit about the leap year bug in 1900)

From 1 March 1900 onward Excel's dates are correct, but if you format the number 1 using the format dddd, mmmm dd, yyyy you'll get the result Sunday, 1 January 1900. That is incorrect; 1 January 1900 was a Monday, not a Sunday. This day of week error continues until you reach 1 March, which is the first truly correct date in the Excel calendar.

Day Zero And Before In Excel

If you use the value zero and display it in date format you'll get the nonsense date Saturday 0 January 1900. If you try to format a negative value as a date, you'll just get a cell full of hash marks. Similarly if you try to obtain a date serial number using Excel functions like DateValue, you can only do so for dates on or after 1 January 1900. An attempt to specify an earlier date will result in an error.

The 1904 (Macintosh) system starts from zero. (1 January 1904 has a value of 0, not 1. Excel's on-line help describes the Mac system as starting from January 2, but that's probably easier than explaining to users why a serial date value of 0 works on the Mac but not Excel.) Negative numbers won't generate an error, but the number will be treated as absolute. That is, both 1 and -1 will be treated as 2 January 1904.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top