How can I format the week as a 2 digit number even if it contains a 0 (01, 02 etc.) - vba

This is the code I currently have:
txtWeek = Format(Date, "ww")
What code can I add so that when I print "txtweek" it shows up as two digits so for the current date as Week 01.

Format it once again.
Format(Format(Date, "ww"), "0#")

Related

Input month as an integer, output the final day of that month

I am trying to write an Excel VBA function that outputs the final day in a month in the format yyyy-mm-dd when given only the month number as an integer.
So for January, a 1 would be input and the output would be 2014-01-31.
I know how to reformat the date using Format(date(), "yyyy-mm-dd") and think I can calculate the last day using DateSerial but am not sure how to input an integer month number into the DateSerial function.
What is the best way to go about this?
Try:
Function: =TEXT(DATE(2014, A1 + 1, 1)-1,"yyyy-mm-dd")
VBA: DateSerial(Year, Month + 1, 1) - 1
This function finds the first day of the next month and then subtracts one day.
Paste into any cell except A1 and input your month in A1 to test.
As a formula, with your formatting, assuming the year is 2014 and your month number is in A1:
=EOMONTH(41639,A1)

Year 2014 getting converted to 1914 in BODS job

The current arrangement is:
SAP DSO: contains the data columns in the form of dd-mm-yyyy.
The BODS job fetches the data from the DSO and loads to the landing table.
The corresponding date column in Teradata is dd-mm-yy.
When the dates are loaded to teradata, year 2014 is converted to 1914.
There is no transformation involved. Direct mapping between the source and target.
This issue started happening only a few months back. Not sure what to check.
To show the correct year in your date, use the DateTime format dd-mm-rrrr.
When dates are stored in one century but refer to another century, dates can be shown with the wrong prefix. The rrrr year format works as so;
If the specified two-digit year is 00 to 49, then
If the last two digits of the current year are 00 to 49, then the
returned year has the same first two digits as the current year.
If the last two digits of the current year are 50 to 99, then the
first 2 digits of the returned year are 1 greater than the first 2
digits of the current year.
If the specified two-digit year is 50 to 99, then
If the last two digits of the current year are 00 to 49, then the
first 2 digits of the returned year are 1 less than the first 2 digits
of the current year.
If the last two digits of the current year are 50 to 99, then the
returned year has the same first two digits as the current year.
I came across a similar issue with BOXI 3.1 and Oracle.
After creating a number of tables with Date fields with the DateTime format as dd/mm/yyyy, and building a universe with this format in mind, I noticed on testing that some of the date results were displaying incorrectly e.g. 01/07/1993 was displaying as 01/07/2093. This was due to the data being loaded into the table having only 2 yy digits e.g. 01/07/93where as Oracle was expecting a DateTime format with 4 yyyy digits.
In turn, Oracle was forcing the year format into 4 digits but as the year was in the last century (20th) but stored in the 21st Century, the wrong century was prefixed onto the year.
To solve, I used the rrrr DateTime Format for the year. Full explaination from Oracle at this link, and further explaination can be found here.
When I recreated the table with the DateTime format as dd-mm-rrrr, the date displayed correctly.
I hope this helps.
DBSControl for CenturyBreak specifies which two-digit years are interpreted as 20th century and which are interpreted as 21st century. If your system has this configured as a non-zero value, which is the default, than this is quite possibly the reason you are seeing the behavior with your data.
CenturyBreak does not affect four digit years or dates input as numeric.
If CenturyBreak=10, strings such as '00/01/01' and '09/01/01' are interpreted as 2000 and 2009. A string inserted as '14/01/01' is interpreted as 1914.
Check with your DBA to determine if the CenturyBreak has been set to a non-zero value or explicitly convert your data inputs to numeric date values.

Excel 2007 Find number of days, based on current date, only if < or > then 2 different dates of same year & month

I have a question for Excel2000 want to do a simple calculation.
I need only one formula if possible include question in the formula to solve it.
Question Condition:
Date 01/Aug/2013 till 31/Aug/2013 in format 08/01/2013 till 08/31/2013 & the current date is 25/08/2013.
Question: Repeated several time for better understanding.
Excel should only do days calculation, based on current date only if the current date is within two date, from 08/01/2013 not less than or greater than 08/31/2013 if <> should remain blank, if within the period to find number of days, for the current month Aug of the year 2013.
Assuming a setup like this:
_____A____ _____B____ ______C_____ _______D______
1 Start Date End Date Today's Date Days Remaining
2 08/01/2013 08/31/2013 08/25/2013 6
This formula in cell D2 should work:
=IF(OR(C2<A2,C2>B2),"",B2-C2)

Dateadd error when subtracting from 0:00

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?
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.

Conditional formating based date range in VB

Have a column of dates which i need to compare to a specific time of the year, which establishes what calculation needs to be applyed then depending on the outcome apply conditional formating to highlight an adjcent cell, but need to remove the YY element as the contents spans multiple years.
if the date in A1 is between 1st Jan & 14th Mar use -8
if the date in A1 is between 15th Mar & 15th Oct use -17
if the date in A1 is between 16th Oct & 31st Dec use -8
Then deduct the above value from B1 to give a target
for each row (from 3 onwards)
if the target is < the value in n then change background orange in f
and repeat.
Any help would be greatly welcome, thank you.
Is this Excel? If so, then it has all kinds of conditional formatting stuff built right into the application; no programming needed.