VBA: AddDate + time changes the format - excel

Can someone advise, please?
I have used DateSerial, DateValue with mixed results, also Format(... doesn't work in this case.
When I use DateAdd("d", 1, DateValue(x.Value))
06/05/2021 20:20:20
is changed to
05/07/2021 20:20:20
All I need is this 06/05/2021 20:20:20 to be changed to this 07/05/2021 20:20:20
FYI if it helps the format of my cells is as follows:

Try this and swap dd/mm to mm/dd (the format will remain dd/mm/yyyy):
Format(DateAdd("d", 1, cel.Value), "mm/dd/yyyy") & " 18:59:59"

If you wish to use DateAdd, and your value is a true date value (not text), use:
DateAdd("d", 1, x.Value)
This will not alter a time part. If x is 2021-05-06 20:20:20, result will be:
`2021-05-07 20:20:20`
If x is 2021-05-06 18:59:59 you can add one hour:
DateAdd("h", 1, x.Value)
for the result: 2021-05-06 19:59:59

just x.value + 1 it'll get that it's a date and add one day

Related

vba, excel - how to add 3 months to a trimmed date

I am terrible with coding dates - I would like to add 3 months to a trimmed date. How would I go about this? Coding example below
Dim AuStart As String
Dim AuEnd As String
AuStart = Trim(HE.CurrentHost.TextRC(10, 19, 8)) 'this would be for example 12/09/19
AuEnd = ??? 'this for example if the AuStart = 12/09/19 should be 3/09/19
HE.CurrentHost.PutText AuEnd, 10, 45
Thanks all
You can directly pass the string as well, but should be in any date format, As third argument of the DateAdd function is a variant variable.
Three Month ahead of AuStartDate
Format(DateAdd("m", 3, AuStart),"mm/dd/yyyy")
Three Month Prior of AuStartDate
Format(DateAdd("m", -3, AuStart),"mm/dd/yyyy")
You can Manipulate the date as per your need, This function is Not just limited to this, For More Detail Please check the below link
[https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function][1]
Like this...
AuEnd = Format(DateAdd("m", 3, DateValue(AuStart)))
Update
You can add the Number Format for the date at the end...
AuEnd = Format(DateAdd("m", 3, DateValue(AuStart)),"mm/dd/yyyy")

Using excel VBA, Is there a way for me to get the name of the current dates day (eg. Monday) [duplicate]

I have a small code that gets the Day Name of the Dates listed in Excel. But the problem is, it's not getting the right Day Name(e.g. Tuesday).
For Example:
sDayName = Format(Day(11/1/2016), "dddd")
Then the output produces an incorrect Day Name which is:
sDayName = "Sunday"
when it's supposed to be "Tuesday".
Thanks for the help guys.
For your specific request, assuming your date string is formatted as your regional setings:
sDayName = Format("11/01/2016", "dddd")
If you want the weekday for today:
sDayName = Format(Date, "dddd")
If you want the weekday for any date in any regional settings
sDayName = Format(DateSerial(2016, 11, 4), "dddd")
'change 2016, 11, 4 for your year, month, day values
:)
WEEKDAY Function can be used on this topic and in Vba Codes . For example:
Label2.Caption = WeekdayName(Weekday(TextBox2, 0), False, 0)
I used this function in when I created userform about date entry to active cell.
i use the answer kadrleyn wrote.
But you have to pay attension that the string returned from
WeekdayName(Weekday(TextBox2, 0), False, 0)
is not trimmed so if you want to use it in if statement you have to use trim function.
ps. i could not add comment to his/her answer because of my reputation.

compare two dates in dd/mm/yyyy format

I have a column where I store dates as dd/mm/yyyy. Then I ask user to input in a form a new date (always as dd/mm/yyyy). I want to compare the input date to the last date that is in the excel already.
If Format(data_mov, "Short Date") < Format(data_last, "Short Date") Then
is not the way to go since it will compare two strings and the test will fail since 03/10/2018 looks smaller than 23/09/2018.
What is the correct way to test them? Something like converting them into timestamps and then compare (but is there something like the unixtimestamp in excel?)
You convert the strings to date and compare them:
' dd/mm/yyyy
' 1234567890
Dim Date1: Date1 = DateSerial(Mid(data_mov, 7, 4), Mid(data_mov, 4, 2), Mid(data_mov, 1, 2))
Dim Date2: Date2 = DateSerial(Mid(data_last, 7, 4), Mid(data_last, 4, 2), Mid(data_last, 1, 2))
If Date1 < Date2 Then
' ...
End If
"What is the correct way to test them?"
IMHO, there is more than one way.
if() with value() will be straightforward.
if() with year(),month(),day(), hour().. is another.
"Something like converting them into timestamps and then compare (but
is there something like the unixtimestamp in excel?)"
The easiest (for me) is using value() function . Then build the desired if() statement on it.
Hope it helps. (:
Microsoft Excel for Windows uses the 1900 date system, by default. Which means the first date is January 1, 1900. a Date and Time function can be used to manipulate the year/date and time/hour/minutes values. The date/time in Excel is stored as a number. Where the decimal part range from 0.0 to 0.99988426 represent 0:00:00 (12:00:00 AM) to 23:59:59 (11:59:59 P.M.), and the integer part range from 0 to 9999 represent year 1900 to year 9999.

Need to Compare Dates in SSRS with Year Parameter and hardcoded Month

I need to compare a date v. "Today" in a MM/YY format and then if true it will execute the rest and if not it will be blank. This is what I have so far but I get the "'Interger' to type 'Date' is not valid...
=IIF(Format(Month(10) & Parameters!Year.Value, "MM/YY") < Format(Today,"MM/YY"),
IIF(Fields!Milestone.Value="Average Number of Days Milestones were Early",
FormatNumber(Fields!Oct.Value,0), IIF(Fields!Oct.Value = 0, "",
FormatNumber(Fields!Oct.Value,0))),"")
I really appreciate any help. Thank you
I kept playing around and got it to work
=IIF(FormatDateTime(CDATE("11" & "/" & Parameters!
Year.Value),DateFormat.ShortDate) < FormatDateTime
(Today,DateFormat.ShortDate),IIF(Fields!Milestone.Value="Average Number of Days Milestones were Early",
FormatNumber(Fields!Oct.Value,0), IIF(Fields!Oct.Value = 0, "",
FormatNumber(Fields!Oct.Value,0))),"")

Subtracting from a date in VBA?

I'm having big problems doing operation with the date in Excel VBA.
I have a form that has a textbox where the user will enter the date. The problem is that he may enter it in different formats (eg, 1.08.2011 for 1st of August, or 8/1/11 for the same day). Now what I want to do is to subtract some days from that date that he enters in the TextBox. I had to success so far and I don't know how to do it.
I tried something like this
Format((Format(Me.datalivrare.Value, "dd.mm.yyy") - 4), "dd.mm.yyyy")
Where datalivrare is that textbox where the user enters the date and 4 is the number of days I want to subtract from that date... and I want the format to always be dd.mm.yyyy no matter what they enter in that textbox.
I suggest looking at the DateAdd function for VBA
http://www.techonthenet.com/excel/formulas/dateadd.php
http://office.microsoft.com/en-us/access-help/dateadd-function-HA001228810.aspx
You could do the following:
Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
First cast to Date, then subtract days, then format appropriately:
Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
the best to add and substract from dates on vba is dateadd() (with negative number for substractions)
also, in your example code there's a missing y on the format string (it accepts 1, 2 or 4 y, only)
It is important to check if the user entered a value that VBA can interprit as a date so first you should:
If isDate(Me.datalivrare.Value) Then
str_Date = Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
Else
MsgBox "Not a valid date value", vbCritical + vbOkOnly, "Invalid Entry"
End If
I think bluefeet's answer had the most information so far and I borrowed the use of DateAdd and CDate.
Just a simple answer, as many aren't using the OP's code.
Eg: Minus 4 days
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function
Sub DateTest()
Dim DateVar As Date
DateVar = DateAdd("d", -4, Date)
Debug.Print DateVar
End Sub

Resources