today's date, now function, macro variable - vba

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

Related

Excel vba get month from Date object

I have a cell that gets filled with a date value. I then store it in a variable.
Sub dateTest()
Dim min_date As Date
min_date = ThisWorkbook.Worksheets("setup").Cells(22, 5).value
MsgBox (min_date)
End Sub
However, I would like to get a month and a day separately from that object. How to do it?
Month() and Day() are the functions that you need:
MsgBox (Month(minDate))
MsgBox (Day(minDate))
Microsoft Month Reference
Microsoft Day Reference
Another way is to use the Format function:
MsgBox Format(minDate, "m")
MsgBox Format(minDate, "d")

VBA Code for Filtering up through the current month

I have a sheet of work orders that have a column of complete by dates. I am trying to filter it down to show all orders with dates in the past going through to the end of the current month (EX: all past orders-End of July 2017).
The current code I'm am using works, but for some reason will not return 7/31/2017. It will return all prior date up to 7/30/2017. Can someone please help.
Sub Macro3()
Dim dtStart As Date
Dim dtFinal As Date
dtStart = CDate(Evaluate("DATE(YEAR(NOW()),-1,1)"))
dtFinal = CDate(Evaluate("EOMONTH(NOW(),0)"))
ActiveSheet.Range("$A$1:$N$709").AutoFilter 13, ">=" & dtStart, xlAnd, "<=" &
dtFinal, Operator:=xlFilterDynamic
End Sub
dtFinal is July 31, 2017 at 12:00am. You will want to make your final date August 1 and then change your filter from "<=" to "<".
You can use the WorksheetFunction.EoMonth function, and the DateSerial, and replace your Now with Date, since you don't need to mess with the hours.
Try the line below instead your entire code:
ActiveSheet.Range("A1:N709").AutoFilter 13, ">=" & DateSerial(Year(Date), -1, 1), _
xlAnd, "<=" & WorksheetFunction.EoMonth(Date, 0), _
Operator:=xlFilterDynamic

Overflow when using DateAdd Excel VBA

I currently have an Excel sheet with a series of dates and a cell in a different sheet with the date when we last ran this macro.
I need to write a macro which checks if today's date is at least 32 days after the day we last ran the macro.
If so, I want to search the sheet with the series of dates and add 10-dates to a dates array.
The dates that we add represent each the closes n*30 days prior to today's date with n which goes from 1 to 10.
So basically 10-dates each representing a multiple of 30-days prior to todays date.
BUT, these dates must be present in the sheet with the series of dates mentioned above so if for example subtracting 30 days from today gives a date which does not exist in the series of dates above, we keep subtracting 1 additional day until we find a date which exists.
Hope this makes sense. I understand it is a bit confusing but I felt I had to give some context.
My code:
Sub date_diff()
Dim todDate
Dim dt
Dim diff As Long
Dim dates(0 To 9) As Date
Dim i As Long
todDate = Format(ActiveWorkbook.Sheets("Overview").Range("B6").Value, "mm/dd/yyyy")
' dt is the Date of last signaling
dt = ActiveWorkbook.Sheets("Overview").Range("B5").Value
diff = DateDiff("d", dt, todDate)
Dim rng As Range
Dim dtCell As Range
Dim currDt
If diff < 32 Then
MsgBox "Wait " & (32 - diff) & " days"
Else
For i = 1 To 10
currDt = Format(DateAdd("d", 20, todDate), "mm/dd/yyyy") ---> OVERFLOW HERE
Set rng = ActiveWorkbook.Sheets("US Stocks").Range("A:A")
' Find the day - Loop until you find one at least 30 days apart
Do While rng.Find(What:=currDt) Is Nothing
currDt = DateAdd("d", -1, currDt)
Loop
dates(i) = currDt
MsgBox i
Next i
End If
End Sub
Run-time error '6':
Overflow
I suppose the error is coming from how I handle or how the dates are interpreted in VBA. SEE CODE FOR OVERFLOW LINE. I am very new to VBA so am still learning these subtleties. FYI, the dates are in the "Short Date" format such as 1/13/15 (mm/dd/yy).

how to substract only working days from a specific date?

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

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