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
Related
This question already has answers here:
Prevent Excel to format string as date
(2 answers)
Closed last year.
Sub test1()
MonthYear = MonthName(Right(2009, 2)) & " " & Left(2009, 2) 'locals window show "September 20"
Range("A1").Value = MonthYear '20-Sep is shown in cell A1 where the formula box is 20-09-2022
Range("A1").Value = "'" & MonthYear '---> my solution for the time being
End Sub
What I want is in cell A1 show September 20 (just like in the Locals Window), as a text not a date.
What I've tried so far :
Sub test2()
MonthYear = "01-" & MonthName(Right(2009, 2)) & "-20" & Left(2009, 2) 'locals window show "01-September-2020"
Range("A1").Value = MonthYear '01-Sep-20 is shown in cell A1 where the formula box is 01-09-2020
Range("A2").Value = Format(MonthYear, "mmmm yy") 'AGAIN, 20-Sep is shown in cell A1 where the formula box is 20-09-2022
Range("A1").NumberFormat = "mmmm yy" 'another solution for the time being
End Sub
My question:
How do I have to write the code, so it put the result in the cell directly as a text September 20 ?
Any kind of help would be greatly appreciated.
Thank you in advanced 🙏.
Apply text formatting first:
Range("A1").NumberFormat = "#"
Range("A1").Value = MonthYear
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)
I am currently using Excel 2010 and I have a column that contains date in such format (dd.mm.yyyy). I would like to change the dots to slash (/). I have tried using rng.Cells(1, 6) = WorksheetFunction.Substitute(rng.Cells(1, 6), ". ", "/")
However, when I do that, the result I get is different from my original date. For example, 02.11.2011 will become 11/02/2011.
The column that contains (dd.mm.yyyy) is in a date format
May I know why is that or is there another way of doing?
Is the column formatted as a date? If so, you can try
rng.Cells(1, 6) = Format(CDate(rng.Cells(1, 6)), "dd/MM/yyyy")
If it's an actual date value, then use NumberFormat
Range("B1").NumberFormat = "d/m/yyyy"
Range("B2").NumberFormat = "m/d/yyyy"
Range("B3").NumberFormat = "m-d-yyyy"
Im using this
splR = Split(oRange.Value, ".")
If UBound(splR) = 2 Then oRange.Value = Format(DateSerial(splR(2), splR(1), splR(0)), RegionalDateFormat) Else oRange.Value = oRange.Value
Function RegionalDateFormat(Optional outS As String)
Dim DateOrder As String
Dim DateSeparator As String
With Application
DateSeparator = .International(xlDateSeparator)
Select Case .International(xlDateOrder)
Case Is = 0
DateOrder = "mm" & DateSeparator & "dd" & DateSeparator & "yyyy"
Case Is = 1
DateOrder = "dd" & DateSeparator & "mm" & DateSeparator & "yyyy"
Case Is = 2
DateOrder = "yyyy" & DateSeparator & "mm" & DateSeparator & "dd"
Case Else
DateOrder = "Error"
End Select
End With
If outS = "Sep" Then outputS = DateSeparator Else outputS = DateOrder
RegionalDateFormat = outputS
End Function
Let's clarify that an Excel spreadsheet does not have a "date" data type per se. Excel's approach to dates is to store an integer, which you can choose to format as a date. So, for example, 43467 is the integer representing Jan 2 2019. (And the decimal part represents time during that day, so 43467.333.. represents Jan 2 2019 8:00 am.)
Try entering a date, then right-click > "Format cells" to format that cell as a number, to observe what I'm talking about.
In the same way, you can cause Excel to render that cell in the date format you want using the same method: select the cell > right-click > "Format cells", choose Custom, and enter a custom format, like dd.mm.yyyy.
Once you see that works, then your remaining task is simply to perform that same action from VBA, which would be with a statement like:
Range("B1").NumberFormat = "dd.mm.yyyy"
... as pointed out by others.
Now, if your initial attempt to get Excel to format your date cell as a number fails (it maintains its date appearance) then the date you see in the cell is actually stored as a string, and won't function as a date (for example to do date arithmetic). So you'll need to decide how to proceed -- perhaps convert these strings to "dates" (ie: numbers formatted as dates).
I have various text boxes in a user form with dates pulled from my worksheets, but all the dates are in US format (m/d/yyyy).
I'm using the ControlSource property to copy the date, e.g.:
ControlSource='SKU Data'!N2
Is there a way to reformat the dates so they appear as d/m/yyyy?
The dates in the worksheets are already formatted to d/m/yyyy.
Do it with Strings:
Sub dural()
txt = "12/25/2014"
a = Split(txt, "/")
txt = a(1) & "/" & a(0) & "/" & a(2)
MsgBox txt
End Sub
I have written a macro which reads the cell content and opens corresponding files as per the cell value.
For example, if I provide 20140522 in a cell, it opens the file "C:\Files\20140522.csv".
However, instead of providing in the above format if I provide it as 22/05/2014 or more generally today's date, how can I convert it into above format in the vba script itself ?. This is because all my files are in the format shown in example only. Following is my macro code
Sub Open()
num=Cells(1,1).Value
ActiveWorkbook.Open(FileName:="C:\Files\" & num & ".csv")
End Sub
You can use Format:
num = Format(Cells(1,1).Value, "yyyymmdd")
Consider:
Sub dural()
Dim d As Date, sDate As String
d = Date
sDate = CStr(Year(d) & Format(Month(d), "00") & Format(Day(d), "00"))
ActiveWorkbook.Open Filename:="C:\Files\" & sDate & ".csv"
End Sub