What exactly happens if I add two dates in VBA - vba

Please help me what happend here- I know it is stupid but I want to know.
The output of the below code is April-18-2105. How did it pop up?
Private Sub CommandButton1_Click()
Dim firstDate As Date, secondDate As Date
firstDate = DateValue("Jun 19, 2010")
secondDate = DateValue("oct 29,1994")
MsgBox (firstDate + secondDate)
End Sub
Thanks in advance!

Dates are actually stored as numbers, so if we look at the numeric value for these dates:
CLng(datevalue("Jun 19, 2010")) '// 40348
CLng(datevalue("oct 29, 1994")) '// 34636
and add them together:
40348 + 34636 = 74984
and convert that number back to a date:
CDate(74984) '// 18/04/2105
Because it's 74,984 days after 00/01/1900

dates are numbers, formatted as dd/mm/yyyy, I think its the days from 31/12/1899, so that's what's happening. The result is 40348+34636=74984, which is 18/04/2105.

Excel stores the date as the number days after a specified date. As such the integer representing the date of 2010/06/19 is 40348 and the integer representing 1994/10/29 is 34636. When you add those integers together you get 74984 which is the integer that represents 2105/04/18.
The reference date does vary from mac to windows. I am running on windows and I did not change my reference date settings, That is where those integers came from. Mac uses January 2nd 1904 as day 1 and Windows uses January 1 1900 as day 1.

Related

populate date time picker from number of days

Am working in windows application.i am scanning my date barcode
i have a date barcode ..while scanning that barcode i am getting value to my textbox like this :
17197...so this value means 2017 july 16,
in that 17-year
197 -calculation of days from 2017 to july 16..
while reading this barcode i want to just populate correct date to datetime picker
how i can convert this value to date time picker..
any help is very appriciable..thanks in advance
If the first two numbers represent the year after 2000 and the following numbers represent the days from the first of january (included) of such year then
Dim test = "17197"
Dim year = Convert.ToInt32(test.Substring(0,2)) + 2000
Dim days = Convert.ToInt32(test.Substring(2))
Dim currentDate = new DateTime(year, 1, 1).AddDays(days-1)
Console.WriteLine(currentDate) ' 16/07/2017

Convert a month name to Integer in VBA, e.g., Jan to 1, Aug to 8

is there a quick way of converting a month name to an integer? So the integer will always be between 1-12, with January =1 and December =12.
The scenario this is used is:
I made a vba which calculates the sales between Jan 2016 and whatever the month this year the client wants to calculate. (so it is not necessary the current month)
My idea is to have an input box once the client runs the VBA, and put the desired month in string.
I understand i could just ask the client to convert it to number, but i am curious if it can be done without much hassles.
I hope this explains.
Thanks,
This isn't locale aware, but should give you the gist:
Public Function MonthNumber(test As String) As Integer
MonthNumber = DatePart("M", test & "/13/2016")
End Function

VBA Set a specific date to be the first week of the year

I want to set a specific date (October 06 , 2014) to be the 1st Week of the year. So when I use the =weeknum() formulaR1C1 it will return the week number.
I'm currently working on a worksheet that gets updated daily so the week number will be updated every week only. In column ("D") indicates the week number. and column ("B") indicates the daily date.
If i use the =weeknum() formula on it returns the value of 41, but it needs to be week number 1.
How about creating your personalized function?
It just consists of counting how many days there are between the date you insert (myDate) and the date which is considered to be the first day of the year (first_day), dividing this number by 7, taking its integer and adding up 1. It will always give you the right one.
Public Function special_weeknum(ByVal myDate As Date) As Integer
Dim first_day As Date: first_day = #10/6/2014#
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
Please note that this approach would allow you also to pass the first day as a user input:
Public Function special_weeknum(ByVal myDate As Date, ByVal first_day As Date)
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
so that you will always be able to decide which is the day you must consider as first day of the year.

Out of a set of data returned grouped by months, only the initial month is returning incorrect data

I am trying to return data for the last 24 months as of the end of last month. Only the initial month returns incorrect data, while all the other months are correct. I believe the issue is in the strBeginDate = section of the code. Any ideas what would be causing partially returned data for the initial month? Thank you very much.
Sub GetStaticApprovalRates_Slide6()
Dim strBeginDate
Dim strEndDate
strEndDate = Sheets("Instructions").Range("EndDate").Value
strBeginDate = DateAdd("m", -23, strEndDate) + 1
Sheets("Slide6Data").Select
It's hard to say exactly what's wrong based only on what you posted. But I do see that you are calculating the start date based on the end date, by only subtracting months. There is no allowance for days. So you might be missing some of that first month by not allowing for the early days of that first month.
That is , if end date occurs mid-month, I think your algorithm would cause start date to be mid-month also. Perhaps missing days 1-x of that first month.

Calculating Months

I have an application where the user selects the dates of a first statement and a last statement. Example, first statement = 1/1/08, last statement = 12/1/08, should equal 12 statements.
However, when using the following code, the result is 11:
numPayments = DateDiff(DateInterval.Month, CDate(.FeeStartDate), CDate(.FeeEndDate))
Is there another way to calculate this, or do I have to be stuck with adding 1 to the result?
Add 1, as you write. ;)
The difference between 1/1/2008 and 12/1/2008 is 11 months. No changing that. ;)
Yes, you'd always have to add one though you may be able to add one to the end date or subtract one from the start date to also get this effect. Consider the case where the start and end dates are the same. Their difference is 0 but you'd still want 1 statement to show just to note one odd case.
Well, the number of months between Jan 1st and Dec 1st is 11... what you're looking for is the difference of months +1. So just add one :)
Also, the DateDiff function you're using is a VB6 hold-over. Better to express it like this:
numPayments = (Date.Parse(.FeeEndDate) - Date.Parse(.FeeStartDate)).TotalMonths + 1
You could try this one. Hope this is very helpful.
Dim myDate As Date
Dim dateNow As Date
Dim nextMonth As Date
myDate = Now
dateNow = Format(myDate, "MM/dd/yyyy")
nextMonth = DateAdd(DateInterval.Month, 5, dateNow) 'compute the next 5 months from date now. Let say, #12/6/2012# the result will be #5/6/2013#
MessageBox.Show(DateDiff(DateInterval.Month, dateNow, nextMonth) & "months==> " & nextMonth)
'This will count the number of months interval. The result will be 5 months=>> #5/6/2013 because we count december to may.