how to substract only working days from a specific date? - vba

assuming that we have a date of 18/06/2015 for example, and we want to substruct 5 days from it, (Saturday and Sunday not included).
I have thought to elaborate an algorihm with DateAdd()but i wanna know wether there is a function that that enables to do so
thanks in advance

Try the workday worksheet function:
WorksheetFunction.WorkDay("2015-06-18",-5)

=WORKDAY(A1,5)
Assuming you have whatever original date in cell A1.

It worked:
Fecha_Inicio = (Extr_DiaAp & " - " & Extr_MesAp & " - " & Extr_YearAp)
'the last was just a Date that i extracted from other cells.
Fecha_Comp = WorksheetFunction.WorkDay(Fecha_Inicio, 25)
'Fecha_Comp is a Date that starts from Fecha_Inicio 25 working days ahead.
MsgBox Fecha_Comp

Related

Check if Date is 2 months prior to today's date

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.

Get past 6 months MONTH in ref to TODAY

Need help in displaying 6 month past month in refrence to todays date. E.g current month is June. below is the code, but is giving the as 1,I need to get January.
MsgBox ("Cheques would be checked for issue Month of " & (Month(Date)-5))
MsgBox ("your date is " & DateAdd("m", -6, Date()))

today's date, now function, macro variable

I currently have an input box for a variable that changes everymonth:
r_mo = Application.InputBox(prompt:="Enter the reporting month as YYYYMM (Eg:201604). Errors in this entry will result in errors in the results.")
This prompts an input box which one has to manually enter into... However, I want to automate this process and eliminate the need for an input box. Isn't there a now function in vba that will automatically generate today's date.
From a now, or system function all I want to do is extract the year in four digits and the month in two digits.
So for example, if we're in decemeber 2016
Sub asasdas ()
"Now function"
r_mo = YYYYMM ' automatically updated from "now function"
End Sub
I appreciate any help you can give me and thank you so much all.
Sub GetMonthYear
GetMonthYear = Year(Now) & Right("00" & Month(Now), 2)
End Sub
That'll do it. Returns the Year portion of Now() and concatenates it to the Month portion of Now(). Since Month will return a single digit for January to September, we wrap this by adding "00" to the Month and take the last two characters from the Right. For "9" we create "009" and take the last two characters for "09". For "10", we would create "0010" and take the last two characters for "10".
Sub GetYearMonthDay()
Dim GetYearMonthDay As String
GetYearMonthDay = Year(Now) & "-" & Right("00" & Month(Now), 2) & "-" & Right("00" & Day(Now), 2)
MsgBox GetYearMonthDay
End Sub
Please refer my answer given below
Sub UsingTheDateFunction()
'this is the standard 6/3/2021 format
Dim theDate As Date
theDate = Date
MsgBox theDate
End Sub

VBA Interval Data

I need to summarize some info depending on the date. I mean, I need to sum some info if the date correspondig to that info is within an interval.
Is there anyway to do it?
I have seen "DATEDIFF", but what I need would be something like:
'If the data evaluted is whitin the next interval sum the value.
Hope you understood my question.
Thanks in advance.
EDITED: I added a pic to make it more understandable
This will do it. I don't know where you want to use total, so now you just get a messagebox.
Sub SumBetweenTwoDates()
Dim total As Integer
total = 0
Dim firstDate As Date, secondDate As Date
firstDate = DateValue("20/11/2012")
secondDate = DateValue("20/12/2012")
For i = 1 To 5
If Range("A" & i).Value >= firstDate And Range("A" & i).Value <= secondDate Then
total = Range("B" & i).Value + total
End If
Next i
MsgBox total
End Sub
Use one formula in the cell you want the sum in:
Assuming the value is in A1:A100 and the dates are in B1:B100
=SUMPRODUCT((B1:B100>=DATEVALUE("1/1/2004"))*(B1:B100<=DATEVALUE("31/1/2004")),A1:A100)
will return the sum of the values fro January 2004

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.