How do I get the whole date difference in VBA?
I know that to get the year or the month or the day I do:
DateDiff("yyyy", Me.DateofBirth, Me.Year).
I want the whole difference. Not just the year, and the two columns that I have are Date of Birth and Year.
Dates are stored internally in VBA as doubles, with the integer portion as the number of days since 1/1/1900. To get the difference between two dates, you can just subtract them:
Dim dob As Date
dob = DateSerial(1990, 1, 1)
Dim difference As Date
difference = Now - dob
Debug.Print Year(difference) - 1900 & " years, " & _
Month(difference) & " months, " & _
Day(difference) & " days."
If you want the total number of days, you can just subtract them and use the numeric value of the underlying double:
Dim dob As Date
dob = DateSerial(1990, 1, 1)
Dim days As Long
days = Now - dob
Debug.Print days & " days."
Related
I want to check if a variable date is in the month that is two months prior to today's date.
Is there a way (using a built in date funciton like dateadd()) to cover the edge cases of when month(now())=1 or month(now())=2 in more elegant manner than the following?
?format(month(now()) & " " & year(now()),"MMMM YYYY")=format(dateadd("M",2,format(varMonth & " " & varYear, "MMMM YYYY")),"MMMM YYYY")
You can use the DateDiff function with the "m" interval to calculate the difference in months between two dates.
I'm trying to find a way of finding a date value, given the WEEKNUM and WEEKDAY
For example
WEEKNUM = 9
WEEKDAY = 4 '(Wednesday)
I can use the below function to find the WEEKNUM from the date, but how can I go the other way around?
Function WEEKNUM(D As Date, FW As Integer) As Integer
WEEKNUM = CInt(Format(D, "ww", FW))
End Function
You could use a table on a hidden sheet, and just used Vlookup to find the correct date. EG
DATE WEEKDAY WEEK NO WEEKDAY + WEEK NO
01/03/2016 Tuesday Week 10 Tuesday 10
02/03/2016 Wednesday Week 10 Wednesday 10
In VBA, you could then look up the name of the sheet, which is the weekday, and got the week number from the filename of the workbook, then use vlookup to find the correct date.
weekNum = Mid(ThisWorkbook.Name, 6, 2)
searchStr = ActiveSheet.Name & " " & weekNum & " " & Format(Now(), "YYYY")
rowNo = Application.WorksheetFunction.Match(searchStr, Sheets("WEEKLIST").Range("D:D"), 0)
strSubject = Format(Sheets("WEEKLIST").Range("A" & rowNo).Value, "DD/MM")
I need to make a function what calculates days until birthday from todays date.
What I have so far is:
Function synnipaev(sk As Date, tana As Date)
synnipaev = DateDiff("d", sk, tana)
End Function
sk is birthdate in the Excel sheet (formated as 10.10.2001 DD/MM/YYYY)
tana is todays date in the Excel sheet ( =TODAY() DD/MM/YYYY)
It gives me the days but also includes the years.
How to make the function not include years?
DateDiff is simply giving you the total number of days between the two dates. You need to find the difference between the current date and the next birthdate:
Public Function DaysToBirthday(birthday As Date) As Integer
Dim targetYear As Integer
'Has the birthday already passed this year?
If Month(Now) > Month(birthday) Or _
(Month(Now) = Month(birthday) And Day(Now) > Day(birthday)) Then
'Then use next year.
targetYear = Year(Now) + 1
Else
targetYear = Year(Now)
End If
DaysToBirthday = CInt(DateSerial(targetYear, Month(birthday), Day(birthday)) - Now)
End Function
Note: VBA stores Date variables as Doubles, with days to the left of the decimal and time to the right. If all you care about are days, you can save the function call and do a simple subtraction.
What I am trying to do is check column AJ for each date that occurs during a year determined by variable "Año". For the dates that are I would like to see how far into the year they are (that is how many days after the first January each date occurs); this is the part that is giving me the error (indicated in my code). What I need is a function to give how many days into the year a date occurs or a better way of writing that line of code.
For Each cl In Workbooks(WbkA).Worksheets("Sheet1").Range("AJ2:AJ1000")
If (cl.Value - ("01/01/" & Año)) > 0 And (cl.Value - ("01/01/" & Año)) < 366 Then 'if it´s in this year
ValueA = ValueA + (cl.Value * ((cl.Offset(0, -13).Value - ("01/01/" & Año)) / 365)) ' this part is giving me the error
End If
End If
Next cl
You are complicating things too much. DateDiff can perform the calculations you want:
Dim iniDate As Date, curDate As Date
Dim ValueA As Integer, Año As Integer
Año = 2010
iniDate = Format(CDate("01/01/" & Año), "MM/dd/yyyy") 'You can change the Format
curDate = Format(CDate("01/05/" & Año), "MM/dd/yyyy")
ValueA = DateDiff("d", iniDate, curDate) 'RESULT -> 4
Dates are stored as the number of days since 1/1/1900, so you can just subtract two dates to get the number of days.
Dim lAno As Long
Dim rCell As Range
Dim dValueA As Double
lAno = 2010
For Each rCell In Sheet1.Range("AJ2:AJ100").Cells
If Year(rCell.Value) = lAno Then 'if it's in the year
'+= some value * the number of days
dValueA = dValueA + (rCell.Offset(0, -13).Value * (rCell.Value - DateSerial(lAno, 1, 1)))
End If
Next rCell
I have a datatable with 2 columns: "amount" and "date"
I want to sum "amount" by month & year.
I am trying this:
_tAmount = myDT.Compute("sum(amount)", "date LIKE '%/" & i & "/" & _year & "'")
where:
var i equals a nº month ( 1 to 12)
var _year equals nº year (example:
2011)
But not displays any results....How I can do it? What is wrong?
I am working with the spanish format (example day/month/year). But I have tried with the english format (month/day/year) and no results too.
This is on VB.NET 2008.
I don't thing date works with LIJKE, why don't you use:
dim startdt as date = new date(_year, month, 1)
dim enddt as date = startdt.addmonth(1).adddays(-1)
_tAmount = myDT.Compute("sum(amount)", "date >= " & startdt & " AND date <= " & enddt)
This is out the top of my head, so check it yourself, but you should get the idea.
Also, I use mostly the # sign around the dates to force english notation, but you'll have to experiment yourself.