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.
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 sheet containing the following formula in cell F10.
I would like to have a VBA code that will calculate this and place the value in cell
=SUMPRODUCT((WEEKDAY(ROW(INDIRECT(F4&":"&F5)),1)=7)*1)
Cell F4 contains a start date and F5 an end date
I tried the following but get a runtime error 438
With ActiveSheet.Range("F10")
.Value = .Evaluate("SUMPRODUCT((WEEKDAY(ROW(INDIRECT(" & Cells(4, 6) & ":" & Cells(5, 6) & ")),1)=7)*1)")
End With
To fix your specific issue Evaluate is not part of the Range. You need to use the worksheet level instead.
Also it is good to return the .Value2 which will strip the format and return the double.
And you do not need the INDIRECT as you are already using a string
With ActiveSheet
Range("F10").Value = .Evaluate("SUMPRODUCT((WEEKDAY(ROW(" & Cells(4, 6).Value2 & ":" & Cells(5, 6).Value2 & "),1)=7)*1)")
End With
To do it solely with vba:
With ActiveSheet
Dim st As Long
st = .Cells(4, 6).Value2
Dim ed As Long
ed = .Cells(5, 6).Value2
Dim cnt As Long
cnt = 0
Dim i As Long
For i = st To ed
If i Mod 7 = 0 Then cnt = cnt + 1 '0 = Saturday, 1 = Sunday, and so on.
Next i
.Cells(10, 6) = cnt
End With
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
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
I'm trying to populate a date from a data source in which the date is formatted as datetime. The destination file can only accept the mm/dd/yy format to upload to our system, but everything that I try is either only cosmetically formatting the date (i.e. datetime still shows in the formula bar) or converts the data to m/d/yyyy which also won't work.
Below is what I've tried, with no success:
Via VBA (only cosmetically changes the format):
[T:T].Select
With Selection
.NumberFormat = "mm/dd/yy"
.Value = .Value
End With
Using VBA to create a temporary helper column U with the below formula (gets me to m/d/yyyy:
=MONTH(T2)&"/"&DAY(T2)&"/"&YEAR(T2)
I know that I can create a bunch of conditional statements to make the above work, but was curious if there was a less convoluted way to solve what seems like a very simple problem.
Edit: To be clear, the result will likely have to be stored as a string.
Per Scott Craner's advice, the following loop worked perfectly!
For i = 2 To LastRow
Range("T" & i).NumberFormat = "#" 'Format as text to prevent excel from taking over
Range("T" & i) = Format(Range("T" & i), "mm/dd/yy")
Next i
Try
Sub test()
Dim rngDB As Range
Dim vDB
Dim i As Long, r As Long
Set rngDB = Range("t1", Range("t" & Rows.Count).End(xlUp))
vDB = rngDB
r = UBound(vDB, 1)
For i = 1 To r
vDB(i, 1) = Format(vDB(i, 1), "mm/dd/yy")
vDB(i, 1) = Replace(vDB(i, 1), "-", "/")
Next i
rngDB.NumberFormatLocal = "#"
rngDB = vDB
End Sub
Try forcing the NumberFormat to text. Here is an example.
Option Explicit
Sub SetDateTime()
Dim lngRow As Long
For lngRow = 1 To 10
Cells(lngRow, 20).Value = Now
Next lngRow
End Sub
Sub ReformatDate()
Dim lngRow As Long
Dim datX As Date
Dim strX As String
For lngRow = 1 To 10
strX = Cells(lngRow, 20).Value
If IsDate(strX) Then
datX = CDate(strX)
Cells(lngRow, 20).NumberFormat = "#"
Cells(lngRow, 20).Value = Format(datX, "mm/dd/yy")
End If
Next lngRow
End Sub