I've got a whole column of 1000+ entries in excel that has the date keyed in as '01/02, '01/03, '01/04... representing Jan 02, Jan 03, Jan 04 and so on as the person was trying to maintain a mm/dd format while saving on the column width.
This has become somewhat troublesome since the entries are all strings instead of dates and I need to input years in now to get dd/mm/yyyy.
Does anyone know how to go about doing this (other than of course manually changing each of the 1000+ entries?)
Also, I would appreciate it if you can share how I can possibly show dd/mm while retaining dd/mm/yyyy on the entry in excel?
Thank you so much for looking at this.
While 393 rows is not an excessive amount of data, you should find that bulk processing is much faster than looping through rows and processing each cell.
Sub Date_Conversion()
With ActiveSheet
With .Columns(1)
.TextToColumns Destination:=.Offset(0, 4), DataType:=xlFixedWidth, FieldInfo:=Array(0, 3)
.Offset(0, 4).NumberFormat = "mm/dd;;;[red]#"
End With
End With
End Sub
As no year was provided for any of the cells, the default is the current year. Any text value that was not processed will be formatted in a red font and left-aligned.
If you wanted to simply replace the existing data in column A, replace .Offset(0, 4) with .Cells(1).
In column 1 let's say you have the dd/mm. In the next column, label that DD and in each cell put
=DAY(A2)
Where a2 is the target cell. Do the same in the next column labelled MM. Create another column labelled YYYY and put the year you want. Then one last column labelled mm/dd/yyyy and put this formula
=CONCATENATE(D2&"/"&C2&"/"&B2)
Long work around, but will get you what you want.
Since the date is represented by a string in the format mm/dd you need to first manipulate the string to get it in a day format:
You can use the below:
=DATEVALUE(RIGHT(CELL,2)&"/"&LEFT(CELL,2))
(replace cell with the address of the cell that you want to change(
Once you have done this to show dd/mm:
Select the cells that you want to format and:
Right Click / Format Cells / Custom
and type in the inputbox dd/mm
Hope this helps.
So I finally fixed it and this charm of a code did the trick! Wanted to share with you guys!
Sub Date_Conversion()
For i = 1 To 393
m = Mid(Cells(i, 1), 1, 2)
D = Right(Cells(i, 1), 2)
Cells(i, 5).NumberFormat = "mm/dd;#"
Cells(i, 5) = DateSerial(2015, m, D)
Next
End Sub
Related
I am looking up a price multiplier for a given date in an Excel sheet, but range.find gives the wrong date. Here are the relevant code snippets:
If Not IsWbOpen("daily_prices.xlsx") Then
Workbooks.Open "C:\.....\daily_prices.xlsx"
End If
Set daily = Workbooks("daily_prices.xlsx").Sheets(1)
daily.Range("A:A").NumberFormat = "dd/mm/yyyy"
End If
This part makes sure that the column A:A, which contains the dates, is formatted correctly.
The code then loops through a date range where d is a date between d1 and d2. In my test script, d1= Jan 1, 2023 (formatted as "01/01/2023"), and d2 = Jan 30, 2023 (formatted as "30/01/2023").
The Excel sheet "daily" has a header in row 1 and 49 different dates in column A, ordered chronologically from "11/01/2023" to "01/01/2024", and the related multiplier (k), starting in row 2.
Only the dates that have a multiplier defined are present in the sheet. The first group of dates is from "11/01/2023" at row 2 to "22/01/2023" at row 13, the next starts with "17/02/2023" at row 14, and so on. Dates in between have no multiplier, and thus are not present.
I search the multiplier with this part of the code:
Set c = daily.Range("A:A").Find(what:=d, After:=Range("A1"), LookIn:=xlValues) 'is this date in the daily prices?
If Not c Is Nothing Then
k = CDec(daily.Cells(c.Row, 4)) ' multiplier is in column 4 (D:D)
rate = rate * k
End If
As expected, c is NOTHING for all dates below "11/01/2023". When d = "11/01/2023", c becomes "not NOTHING" but strangely it returns "11/11/2023" instead of "11/01/2023", which is in row 40 instead of row 2, thus it fetches the multiplier of row 40 instead of row 2.
I have re-checked all the code after reading several similar cases and made sure that column A is formatted correctly as "date", but I still get "11/01/2023" wrongly 'found' at row 40 instead of row 2, and the value returned is "11/11/2023" instead of "11/01/2023".
Instead of "12/01/2023", it finds "31/12/2023", and instead of "14/01/2023", which is part of the dataset, it finds nothing.
What did I overlook, where is the error?
Eventually, I found the solution.
It appears that using dates formatted as dates leads to ambiguity with Range.Find(d) as it's very similar to a text search, and depending what date format you use, their partial strings (month and day) may create confusion in certain language versions of Windows. I noticed this when I was looking for a date with day=11 or 12 and found a date with month=11 or 12. This is typical for the confusion that occurs sometimes when you use data formats like "mm-dd-yyyy" (US) and "dd/mm/yyyy" (other) and looks like a Windows bug to me. Windows should distinguish the two formats clearly, but sometimes it doesn't.
There was a similar problem at VBA Range.Find method not finding a value that IS in the range that helped me find the solution.
I needed to format the search object temporarily as numbers
daily.Range("A:A").NumberFormat = "0"
and also search for the long integer value of the running date, thus the starting value is
d = CLng(date1)
where d is type number and date1 is type date. The search term is then simply
Set c = daily.Range("A:A").Find(d)
If Not c Is Nothing Then ...
At the end I reformat the date column with
daily.Range("A:A").NumberFormat = "dd/mm/yyyy;#"
This resolved the problem.
Thank you all for your kind help.
#Ron Rosenfeld's hint came closest as he proposed to search for What:=CDbl(date), which is what I am basically doing, except that I prefer CLng over CDbl as I need the date as an integer and not as a floating point number. The only problem with that idea was that you cannot search for .value2. You can only search for xlValues (which is not the same) or xlFormulas. Neither would find this long integer number in the cell's properties.
Transforming both the search object and the search item to numbers resolves the ambiguity completely.
I always get the dates like shown in Column A, but I need them as shown in column C. How can I do it without having to create column B helper? I need that because the hours and minutes mess up what I want to achieve after this.
Column A
Time with hours and minutes:
22/05/2015 14:57
11/06/2015 15:40
28/05/2015 08:27
26/05/2015 14:51
25/05/2015 14:18
25/05/2015 15:51
Column C
Time without hours and minutes:
22/05/2015
11/06/2015
28/05/2015
26/05/2015
25/05/2015
25/05/2015
I managed to solve this by creating a column B that converts to text
=Text(A2;"DD-MM-AAAA")
and then column C converts text to value
=Data.value(B2)
While this process works, it causes me a lot of unnecessary data and I wonder if I can do this automatically with a vba-excel macro in one step. I also want the number dates in column C always equal to the number of dates in column A and dates in the same order.
To get the date only use:
=INT(A2)
And format to date. This works because dates in excel are the number of days since December 31st, 1899 so for example May 10th, 2016 is 42500 days since.
And the Time is a decimal based on 24 hours. So, May 10th, 2016 12:00:00 equates to 42500.5.
So to get just the date we remove the decimal. INT() does just that.
This was tagged with [excel-vba] so I'll offer a full column time stripping solution.
The Range.TextToColumns method makes quick work of stripping off the date portion of a datetime. The fieldinfo parameter can specify the DMY format you are using and discard the time portion with the appropriate xlColumnDataType. While most TextToColumns commands are made 'in place', an alternate destination is available if specified. When specifying an alternate destination, the original data is left intact.
Option Explicit
Sub stripTime()
Dim i As Integer
With Worksheets("Sheet1")
'check to see if A1 is a date or a column text label
i = Abs(Not IsDate(.Cells(1, 1).Value))
With .Range(.Cells(1 + i, "A"), .Cells(.Rows.Count, "A").End(xlUp))
.TextToColumns Destination:=.Cells(1, "C"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, xlDMYFormat), Array(10, xlSkipColumn))
'optionally assign a custom number format
.Offset(0, 2).NumberFormat = "[color10]dd-mmm-yyyy_)"
'optionally autofit the column width
.Offset(0, 2).EntireColumn.AutoFit
End With
End With
End Sub
After processing you are left with your original datetimes in column A and true date-only values in column C.
you can use =Today() OR =NOW() then change
10-04-2020 3:09:42 PM transforms to 10.04.2020
Using this formula:
=IF(ISBLANK(A1),"",TEXT(A1,"DD.MM.YYYY"))
My code below has a vlookup which changes a cell in my data (CurrentMonth), but I can't get it to keep the formatting of the source. The return values in col 2 are 01, 02, 03 etc up to 12 in general text format, but the formula below returns 1, 2, 3 etc.
I've tried putting a ' before the numbers in the table I'm looking into, but that doesn't work. Also tried copying the format with code and then pastespecial formats on the changed value, but this doesn't work. The format type of the lookup table and the data I'm changing are identical. What code do I need to keep the formating please so I don't lose the 0 at the beginning, keeping the lookup value as text?
'vlookup based on combobox selection held in variable
Cat3No = Application.WorksheetFunction.VLookup(Me.ComboBoxCat3Name.Value, (Worksheets("ValidCombos").Range("C:D")), 2, False)
'variable used to change cell in dataset
Worksheets("CurrentMonth").Cells(Currentrow, 32).Value = Cat3No
If you don't need the values to be text, you could format the cells as a Custom format of Type "00":
EDIT
A simpler answer is to change your code to this:
'vlookup based on combobox selection held in variable
Cat3No = Application.WorksheetFunction.VLookup(Me.ComboBoxCat3Name.Value, (Worksheets("ValidCombos").Range("C:D")), 2, False)
'variable used to change cell in dataset
With Worksheets("CurrentMonth").Cells(Currentrow, 32)
.Value = Cat3No
.NumberFormat = "00"
End With
In excel, I have reports that give me a column of dates (m/d/yy in Column A) and a column of times (hh:mm:ss in Column B). I have an issue with the dates being listed as the previous day if the time is before "07:00:00". To fix this issue, I normally run an IF statement in the "C" column that says =IF(B2<$C$1,A2+1,A2). In cell "C1" I manually put in the "07:00:00" and start the IF statement at C2 and copy it down to the rest of the entries. I also note that when I get this report in excel, the time column is set to the format of "General".
I am running into an issue trying to make this run in a macro so I do not have to take the extra time to do this (and some other) formatting. I have gotten the IF statement into the cells I want but none of the dates that are incorrect are changing. This seems to be because of the formatting of the times (and possibly dates?) . I attempted to use the .NumberFormat = "hh:mm:ss" to make sure that all of the referenced and new cells were the same but there is still no change. Am I missing something to make the formats match up? Here is a sample of the code I am using:
Sub Date_Change()
Dim w1 As Worksheet
Set w1 = Worksheets("Sheet1")
w1.Range("C1").FormulaR1C1 = "07:00:00"
w1.Range("C1").NumberFormat = "hh:mm:ss"
w1.Range("A:A").NumberFormat = "m/d/yy"
w1.Range("B:B").NumberFormat = "hh:mm:ss"
w1.Range("C2").Formula = "=IF(B2<$C$1,A2+1,A2)"
w1.Range("C2").NumberFormat = "m/d/yy"
w1.Range("C2").Select
ActiveCell.Copy
ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1),
ActiveCell.Offset(0,-1).End(xlDown).Offset(-1, 0)).Offset(0, 1)
End Sub
Can you start C1 with,
w1.Range("C1") = TimeSerial(7, 0, 0)
... and follow up with,
w1.Range("C1").NumberFormat = "hh:mm:ss"
That will definitively put in a correct 7:00 AM starting time (e.g. 0.291667).
The IF formula as you have provided for C2 might be better as,
w1.Range("C2").Formula = "=A2 + (B2<$C$1)"
I have been searching for about an hour on how to do this in Excel.
I have an Excel file that was created from an old system and I am pulling information from a SQL Server Database, I will be inputting the information back into the SQL Server Database and would like the Dates to match.
I have tried Creating a Custom Format, but I am unsure if I even did it Correctly. I found several places where they want to go the other way mm/dd/yyyy to yyyymmdd but they have not been helpful.
I am unfamiliar with using VBA in any Microsoft Products otherwise I am sure that this would be a simple Task.
I have two separate columns that need to be changed.
How do I Format the entire column from (float)yyyymmdd to a (Date)mm/dd/yyyy
You can convert the value to a date using a formula like this, next to the cell:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
Where A1 is the field you need to convert.
Alternatively, you could use this code in VBA:
Sub ConvertYYYYMMDDToDate()
Dim c As Range
For Each c In Selection.Cells
c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2))
'Following line added only to enforce the format.
c.NumberFormat = "mm/dd/yyyy"
Next
End Sub
Just highlight any cells you want fixed and run the code.
Note as RJohnson mentioned in the comments, this code will error if one of your selected cells is empty. You can add a condition on c.value to skip the update if it is blank.
Do you have ROWS of data (horizontal) as you stated or COLUMNS (vertical)?
If it's the latter you can use "Text to columns" functionality to convert a whole column "in situ" - to do that:
Select column > Data > Text to columns > Next > Next > Choose "Date" under "column data format" and "YMD" from dropdown > Finish
....otherwise you can convert with a formula by using
=TEXT(A1,"0000-00-00")+0
and format in required date format
Here is a bare bones version:
Let's say that you have a date in Cell A1 in the format you described. For example: 19760210.
Then this formula will give you the date you want:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2)).
On my system (Excel 2010) it works with strings or floats.
for converting dd/mm/yyyy to mm/dd/yyyy
=DATE(RIGHT(a1,4),MID(a1,4,2),LEFT(a1,2))
Found another (manual) answer which worked well for me
Select the column.
Choose Data tab
Text to Columns - opens new box
(choose Delimited), Next
(uncheck all boxes, use "none" for text qualifier), Next
use the ymd option from the Date dropdown.
Click Finish