I have dates in the following format :-
01.01.2019
01.02.2019
01.03.2019
14.03.2019
and I would like these in UK date format of dd/mm/yyyy, using VBA conversion.
I attempted to use a REPLACE, changing "." to "/", but this outputs the dates as :-
01/01/2019
02/01/2019
03/01/2019
14/03/2019
Is there a way to overcome this?
The below code loop column A & convert the date in column B. Try:
Option Explicit
Sub test()
Dim Year As String, Month As String, Day As String, WholeString As String
Dim i As Long, Lastrow As Long
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
WholeString = .Range("A" & i).Value
Year = Right(WholeString, 4)
Month = Mid(WholeString, 4, 2)
Day = Left(WholeString, 2)
With .Range("B" & i)
.Value = CDate(Day & "/" & Month & "/" & Year)
.NumberFormat = "dd/mm/yyyy"
End With
Next i
End With
End Sub
What does this do for you? I'm curious if it would return dd/mm/yyyy for you:
With ActiveSheet.UsedRange.Columns("A").Cells
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, FieldInfo:=Array(0, xlDMYFormat)
.NumberFormat = "dd/mm/yyyy"
End With
Related
I have the following text in cell A1
09-03-22
that's mm-dd-yyyy
the type is general but I want to Convert it into Date with the format dd.mm.yyyy
Can be in Excel or with vba....
Because if I change the type to date it always returns as 09.03.2022 or 09 March 2022 ... Excel thinks my month is my day and the other way around. But what I want is 03.09.2022
With VBA, this is one way to do it in the ActiveCell:
Sub TxtDateToDate()
Dim strDate As String
With ActiveCell
strDate = .Value
strDate = Split(strDate, ".")(1) & "/" & _
Split(strDate, ".")(0) & "/" & _
Split(strDate, ".")(2)
.Formula = .Value + 0 'shake cell format
.Formula = DateValue(strDate)
.NumberFormat = "MM.DD.YYYY"
End With
End Sub
If I correctly understood your case, it looks that there is a column having inconsistent Date/String data. The existing Date swaps the day with month and the string date starts with month followed by day.
If this is the case, please try the next code. It assumes that the column to be processed is A:A, and returns in column B:B. You can easily change the two columns:
Sub ConvertText_DateToDate()
Dim sh As Worksheet, lastR As Long, arr, arrD, arrStrD, i As Long
Set sh = ActiveSheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
arr = sh.Range("A2:A" & lastR).Value2
ReDim arrD(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
If IsNumeric(arr(i, 1)) Then
arrD(i, 1) = DateSerial(Year(arr(i, 1)), Day(arr(i, 1)), Month(arr(i, 1)))
Else
arrStrD = Split(arr(i, 1), "/")
arrD(i, 1) = DateSerial(CLng(arrStrD(2)), CLng(arrStrD(0)), CLng(arrStrD(1)))
End If
Next i
'format and drop the processed array content, at once:
With sh.Range("B2").Resize(UBound(arrD), 1)
.NumberFormat = "dd-mm-yyyy"
.Value2 = arrD
End With
End Sub
But, if the Date part did not swap day with month, you have to use
arrD(i, 1) = arr(i, 1)
Instead of:
arrD(i, 1) = DateSerial(Year(arr(i, 1)), Day(arr(i, 1)), Month(arr(i, 1)))
I have a date in cell A1 for example: 12/08/22
I want create a list with all days of the month (1-31) in column E using the month and year of the cell A1 as parameter.
Sub TESTEEEEEEE()
Dim r As Range, i As String, j As Long
i = Range("A1").Offset(0, 1).Value = Left(cell.Value, 3)
'k = ????
j = 31
For Each r In Range("E1:E31")
r.NumberFormat = "#"
r.Value = Format(DateSerial(k, i, j), "dd/m/yy")
j = j + 1
Next r
End Sub
I'm stucked in how to extract the the month and year. I was trying using the position of the characteres as parameter, but i'm not getting it work.
i should extract the 4,5 and characterer returning 08 (ok, the code is wrong i was making some tests).
k should extract the 7,8 charachter returning 22.
Someone can help me?
Please, try using the next way. It does not need iteration:
Sub testCreateWholeMonth()
Dim D As Date, lastD As Long
D = Range("A1").value
lastD = Day(WorksheetFunction.EoMonth(DateSerial(Year(D), Month(D), 1), 0))
With Range("E1:E" & lastD)
.value = Evaluate("date(" & Year(D) & "," & Month(D) & ",row(1:" & lastD & "))")
.NumberFormat = "mm.dd.yyyy"
End With
End Sub
You are probably after calendar functions like YEAR, MONTH, EOMONTH
Sub DebugDate()
Dim rg As Range
Set rg = Range("A1") ' should contain a date
Dim dt As Date
dt = rg.Value
Debug.Print Year(dt), Month(dt), CDate(WorksheetFunction.EoMonth(dt, 0))
' End of month not using worksheet function EOMONTH
Debug.Print DateSerial(Year(dt), Month(dt) + 1, 1) - 1
End Sub
Further reading on How to create a calendar with VBA
My worksheet has these formulas. Some of the values in column T are Date and some are string or general:
And its values:
I have this VBA code that loops through each cell in a column and convert the string value to a date and format the date. However, it is working perfectly fine with a double-digit month and day, but not single digits. What is the cause of problem and how can I make it to always result in double digit, i.e "01 - 02 -2021", not "1/2/2021"?
Sub Date_format()
Dim Cel As Range
Dim i As Integer
i = ActiveWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
' First I applied mm - dd as the same as the work sheet
ActiveSheet.Range("T2:T" & i).NumberFormat = "mm-dd-yyyy"
For Each Cel In ActiveSheet.Range("T2:T" & i)
If Not Cel.Value = "n/a" Then
' Then I applied dd - mm to swap the day and month
Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
End If
Next
End Sub
Once I applied .NumberFormat = "dd-mm-yyyy" to the range, my other formula =DAYS(TODAY(),T2) that calculate days, are not working any more on most cells as shown in this picture:
Please, try transforming this part:
If Not Cel.Value = "n/a" Then
Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
End If
as:
If Not Cel.Value = "n/a" Then
Cel.NumberFormat = "dd - mm - yyyy"
Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
End If
As I mentioned in the comments above, add
.Range("T2:T" & i).NumberFormat = "dd - mm - yyyy"
before the FOR loop.
Here is another way to achieve what you want without using loops.
Code:
Option Explicit
Sub Date_format()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Last row in Col T
lRow = .Range("T" & .Rows.Count).End(xlUp).Row
With .Range("T2:T" & lRow)
.NumberFormat = "dd - mm - yyyy"
.Value = ws.Evaluate("index(IF(" & .Address & _
"<>""n/a"",DATEVALUE(" & .Address & "),""n/a""),)")
End With
End With
End Sub
Screenshot:
Explanation:
This approach uses EVALUATE with INDEX, IF and DATEVALUE to do the conversion without using any loops. For explanation on how it works, please see Convert an entire range to uppercase without looping through all the cells
This the data I have.
I just want its month name and year.
For example 201804: Apr 2018
Try this UDF
Sub Test()
Debug.Print FormatDate("201804")
End Sub
Function FormatDate(sInput As String)
sInput = "1/" & Right(sInput, 2) & "/" & Left(sInput, 4)
FormatDate = Format(CDate(sInput), "mmm yyyy")
End Function
Try
Sub test()
Dim vDB
Dim i As Long
Dim s As String, y As String, m As String
vDB = Range("a1", Range("a" & Rows.Count).End(xlUp))
For i = 2 To UBound(vDB, 1)
s = vDB(i, 1)
y = Left(s, 4)
m = Right(s, 2)
vDB(i, 1) = Format(DateSerial(y, m, 1), "mmm yyyy")
Next i
With Range("b1").Resize(UBound(vDB, 1), 1)
.NumberFormatLocal = "#"
.Value = vDB
End With
End Sub
Here is the worksheet formula, converting the date in A1. Copy the formula down as required.
=DATE(LEFT(A1,4),RIGHT(A1,2),1)
The result is a proper date set for the first day of the month, in this case April 1, 2018. The format in which this result is displayed depends entirely upon your system settings. However, you can over-ride the system by setting a custom date format in Format > Cells > Numbers > Custom*. Set Type as mmm yyyy to display Apr 2018 in the cell.
I have a column which has date and time of emails from outlook. Some dates are in format - January 2, 2020 4:15 PM, January-14-20 12:44 PM, December-24-19 20:15 PM.
I have tried to use Replace and Substitute functions, Replace does work as defined but from the string time is removed. I would like to have all dates as 2019-12-27 3:02 PM.
sub Replace()
Dim sString, searchString as String
dim i, LastCell as Long
LastCell = Range("C" & Rows.Count).End(xlUp).Row
Set searchRng = Range("C3:C" & lastCell)
For Each c In searchRng
sString = c
searchString = "-"
If InStr(1, sString, searchString, vbTextCompare) > 0 Then
i = InStr(1, sString, SearchString, vbTextCompare)
i = InStr(i + 1, sString, SearchString, vbTextCompare)
c.Offset(0, 1) = WorksheetFunction.Replace(sString, i, "19", " 2020")
End If
Next c
End Sub
A really safe way to do it without letting Excel guess:
Use a Regular Expression to parse and split the date into pieces (eg using this pattern):
And then use the DateSerial function and TimeSerial function to create a real date value:
Dim RealDateTime As Date
RealDateTime = DateSerial(2020, 2, 20) + TimeSerial(16, 50, 22)
That can be written as real date value into a cell:
Range("A1").Value = RealDateTime
And formatted how ever you like (because it is a real date value)
Range("A1").NumberFormat = "yyyy-mm-dd hh:mm AM/PM"
CDate will convert a date/time that is a string into a real date/time that can be formatted.
So code like:
For Each c In rngSrc
c.Offset(0, 1) = CDate(c)
c.Offset(0, 1).NumberFormat = "yyyy-mm-dd h:mm AM/PM"
Next c
will convert
I still think you could do this easier:
'
'
'
LastCell = Range("C" & Rows.Count).End(xlUp).Row
With Range("C3:C" & lastCell).Offset(0, 1)
.FormulaR1C1 = "=TEXT(RC[-1],""yyyy-mm-dd hh:mm AM/PM"")"
.Value = .Value
End With
NOTE: yyyy-mm-dd hh:mm AM/PM will work according to your regional settings, so if Excel is not in english, translate it. My excel is spanish, and this code would not work for me unless I type aaaa-mm-dd hh:mm AM/PM instead of yyyy-mm-dd hh:mm AM/PM
Convert all dates at once.
Range