Write date to cell and change date format - excel

I have some issue with my VBA code. I got Date variable that stored as dd/mm/yyyy but when I write the date to a specific cell the format change to mm/dd/yyyy, I have been trying many options but none of them worked for me, I also check that the variable know what is the current day, month and year.
The code:
Sheets("report_orders").Select
Range("A1").Value = customerName
Range("A2").Value = "äæîðä " & orderID
Range("A3").Value = "äæîðä ì÷åç " & orderPo
'Range("B2").Value = orderDate
Range("B2").Value = Day(orderDate) & "/" & Month(orderDate) & "/" & Year(orderDate)
Debug.Print "year " & Year(orderDate)
Debug.Print "month " & Month(orderDate)
Debug.Print "day " & Day(orderDate)
but the result is :

When you put a date into a cell, you can format it using .NumberFormat. Ensure that you format the cell before entering the value. For example
Range("B2").NumberFormat = "mm/dd/yyyy"
Range("B2").Value = orderDate
You can read more about the .NumberFormat in Range.NumberFormat property (Excel)

Related

Verify date format in textbox entry

I wish to provide a textbox in excel vba to enter the date. Permissible date formats are d-m-yy or yyyy, dd-mm-yy or yyyy, d/m/yy or yyyy, dd/mm/yy or yyyy. But the output format should be dd-mm-yyyy. I just don't know where to start. I know how to write the data to the required except worksheet but no idea about this code.Please guide
If IsDate(Me.TextBox2.Value) = False Then.
MsgBox "Enter a valid date format." & vbNewLine _
& "Valid Date Formats are:" & vbNewLine & vbNewLine _
& "D/M/YY" & vbNewLine _
& "DD/MM/YYYY" & vbNewLine _ & "D-M-YY" & vbNewLine _
& "DD-MM-YYYY", vbExclamation, "Date Format ERROR"
TextBox2.Activate
Exit Sub
End If
'This works perfect
Worksheets("PURCHASE").Range("B" & newrow) = CDate(TextBox2.Text)
Worksheets("PURCHASE").Range("B" & newrow).NumberFormat = "dd-mm-yyyy"
There is a VBA Range.IsDate function that attempts to resolve a valid date as true and an invalid date as false.
In Windows, the range of valid dates is January 1, 100 A.D., through December 31, 9999 A.D.; the ranges vary among operating systems.
That doesn't explain the Excel 1900 date system but it does explain the Excel 1904 date system. Blame Lotus 1-2-3 for the inequality.
Whatever algorithm is used in IsDate, I can say that I've had marginally more success when using it than a CDate conversion over a wide variety of system locales both on the source and target end.
Ultimately I've found the SQL standard YYYYMMDD an efficient storage and delivery method as well as raw long integers. The latter would simply require cell formatting in Excel to show a real human being that the value is a date.
If your code accepts the string date input as verified by CDate or IsDate then you may be transferring a string date into your cell. Dates are numeric and may need to be formatted in order for a human being to see them as dates and not a number like 43453.
Worksheets("PURCHASE").Range("B" & newrow) = cdate(TextBox2.Text)
Worksheets("PURCHASE").Range("B" & newrow).numberformat = "dd/mm/yyyy"
An option would be to, after the user entered a string into the textbox, to check whether it is possible to format the date. If not, a nonsense-date has been added and the user can be notified of the issue
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim nwdate As String
If Len(TextBox1.Value) < 6 Then GoTo falsedate
nwdate = Format(TextBox1.Value, "dd-mm-yyyy")
If nwdate = TextBox1.Value And Format(nwdate, "dd-mmmm-yyyy") = nwdate Then
GoTo falsedate
Else
TextBox1.Value = nwdate
End If
Exit Sub
falsedate:
MsgBox "Enter a valid date before proceeding", vbExclamation, "Error"
TextBox1.Value = ""
End Sub

Reformatting date & time does not change after using VBA macro

I am trying to take a file with the date being mm/dd/yyyy and reformat it to yyyy-mm-dd and output that new format to a new sheet, but even after running my VBS macro it does not change although the birthDate is correct. I have seen that there is issues with Excel overwritting your format to US regardless. Is there any way around this? Thanks!
birthDate = Trim(CStr(Sheet1.Cells(currRawRow, "F")))
checkInput = InStr(1, birthDate, "/")
If (checkInput > 0) Then
birthYear = Year(birthDate)
birthMonth = Month(birthDate)
birthDay = Day(birthDate)
birthMonth = Format(CStr(birthMonth), "00")
birthDay = Format(CStr(birthDay), "00")
birthYear = Format(CStr(birthYear), "0000")
birthDate = (birthYear & "-" & birthMonth & "-" & birthDay)
Any number recognised as a date by Excel will display in the short date format of the control panel regional settings ->Date & Time for whomever opened the file.
To override this, either:
1) set the format explicitly as part of you loop as Daimian suggests or;
2) force the format to string, by preceding the date by a single quote.
e.g in your last line of code change:
birthDate = (birthYear & "-" & birthMonth & "-" & birthDay)
to
birthDate = "'" & (birthYear & "-" & birthMonth & "-" & birthDay)

How to get current month?

I can't get the current month.
It seems very simple to get the current year and day, as tested with the following:
MsgBox Year(Date)
MsgBox Day(Date)
MsgBox Year(Now)
MsgBox Day(Now)
How is it possible to show the current month as either a number (1, 2 etc.) or a full name?
I could use TODAY() in a cell and convert that in VBA with something like CurrentMonth = MonthName(Month(Sheet1.Range("A1"))) but I would like to do this directly in VBA for Excel.
Try,
debug.print Format(Date, "mmm") 'Mar
debug.print Format(Date, "mmmm") 'March
debug.print Format(Date, "m") '3
debug.print Format(Date, "mm") '03
Month(Now)
Returns the index number associated with the current month.
Jeeped's code below is the most compact, but to give you an idea of how indexes work, the following code will return the month name based on the index returned:
Dim months(11) As String
months(0) = "Jan"
months(1) = "Feb"
months(2) = "Mar"
months(3) = "Apr"
months(4) = "May"
months(5) = "Jun"
months(6) = "Jul"
months(7) = "Aug"
months(8) = "Sep"
months(9) = "Oct"
months(10) = "Nov"
months(11) = "Dec"
Dim nowMonth As Integer
nowMonth = Month(Now)
For i = 0 To 11
If nowMonth = (i + 1) Then
MsgBox (months(i))
End If
Next
Found an easier solution to get the current Month Name
Just use MonthName(Month(Now)) and assign it to a string.
Month(Now) gives you the month number and MonthName() uses that number to display the current month
A really helpful and simple way is to combine the format function together with date.
Examples (assuming today is Oct 23, 2019):
To get current month as a number as in original question:
MsgBox Format(Date, "mm")
^ Will return: 10
To get current month as short text:
MsgBox Format(Date, "mmm")
^ Will return: Oct
To get current month with full text:
MsgBox Format(Date, "mmmm")
^ Will return: October
You can combine these with days and years as well.
Additional examples:
MsgBox Format(Date, "dd-mmm-yyyy")
^ Will return 23-Oct-2019
MsgBox Format(Date, "dddd-mmmm-dd-yyyy")
^ Will return: Wednesday-October-23-2019
This is creating a custom format, so you can rearrange the dd, mm, yyyy areas as you see fit, such as:
MsgBox Format(Date, "yyyy/mm/dd")
^ Will return: 2019/23/10
Here is the best way I have found to do it:
Sub getMonth()
'MsgBox DatePart("m", Date)
'MsgBox Evaluate("MONTH(""" & Date & """)")
'MsgBox VBA.DateTime.Month(Date)
MsgBox Format(Date, "mmmm")
End Sub
Below is how I found the previous month based on the current month name, the assignment to monthNum is the piece needed to solve your question.
month = "February"
'****'
monthNum = Application.Evaluate("=MONTH(1&" & Chr(34) & month & Chr(34) & ")") 'Returns month #
'****'
If monthNum = 1 Then
monthNum = 12
Else
monthNum = monthNum - 1
End If
month = MonthName(monthNum) 'Returns January

Excel: Value and date in same cell

the question is: is there a way to store a value, eg. string "CODE1", and a date in the same cell?
example: A1.value="CODE1" and A1.date=01/01/2014
If you need to put the two value visible in the cell, you can use:
Range("A1").Value = "CODE1 " & Format(Now(), "dd/mm/yyyy")
if you need to add an Hidden second value, you can use the ID:
Range("A1").Value = "CODE1"
Range("A1").ID = Format(Now(), "dd/mm/yyyy")
MsgBox Range("A1").Value
MsgBox Range("A1").ID

Excel Macro : How can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?

I am using DateTime.Now in my Excel Macro to show the current timestamp.
It shows timestamp in "dd-MM-yyyy hh:mm:ss" format.
Instead, how can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?
Try with: format(now(), "yyyy-MM-dd hh:mm:ss")
DateTime.Now returns a value of data type Date. Date variables display dates according to the short date format and time format set on your computer.
They may be formatted as a string for display in any valid date format by the Format function as mentioned in aother answers
Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Format(Now(), "yyyy-MM-dd hh:mm:ss")
If some users of the code have different language settings format might not work. Thus I use the following code that gives the time stamp in format "yyymmdd hhMMss" regardless of language.
Function TimeStamp()
Dim iNow
Dim d(1 To 6)
Dim i As Integer
iNow = Now
d(1) = Year(iNow)
d(2) = Month(iNow)
d(3) = Day(iNow)
d(4) = Hour(iNow)
d(5) = Minute(iNow)
d(6) = Second(iNow)
For i = 1 To 6
If d(i) < 10 Then TimeStamp = TimeStamp & "0"
TimeStamp = TimeStamp & d(i)
If i = 3 Then TimeStamp = TimeStamp & " "
Next i
End Function
this worked best for me:
Cells(partcount + 5, "N").Value = Date + Time
Cells(partcount + 5, "N").NumberFormat = "mm/dd/yy hh:mm:ss AM/PM"
Copy and paste this format yyyy-mm-dd hh:MM:ss in format cells by clicking customs category under Type
Timestamp in saving workbook path, the ":" needs to be changed. I used ":" -> "." which implies that I need to add the extension back "xlsx".
wb(x).SaveAs ThisWorkbook.Path & "\" & unique(x) & " - " & Format(Now(), "mm-dd-yy, hh.mm.ss") & ".xlsx"
It can work as easy as this, choose the location you want, in this case I choose D3
Sheets("Put your Sheet's name here").Range("D3") = Now
Example, my sheet is called Sources
Sheets("Sources").Range("D3") = Now
Use the Format function.
Format(Date, "yyyy-mm-dd hh:MM:ss")

Resources