lidorag
lidorag

Reputation: 69

Write date to cell and change date format

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)

date value

but the result is :

enter image description here

Upvotes: 1

Views: 846

Answers (1)

braX
braX

Reputation: 11755

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)

Upvotes: 4

Related Questions