So I have this simple issue, my date format is inputted on sheet as dd/mm/yyyy, but when I use this code below, it keeps printing mm/dd/yyyy for cases when day is lesser than 12 (if date is inputted as 31/10/2022 for example, excel understands that first number is day, I'm assuming it's because there is no month 31). I want to stop this from happening, since I need my date to be on local configuration. For some reason, using .FormulaLocal is returning stacked overflow error.
Input: 01/07/2022 on range A2
20:08:00 on range K2
Code:
Cells(2, 24) = Application.WorksheetFunction.Text(Cells(2, 1), "dd/mm/yyyy") & " " & Application.WorksheetFunction.Text(Cells(2, 11), "hh:mm:ss")
Output: 01/07/2022 20:08:00
Related
I just want to create list of months automatically between start and end dates in which if the starting date or the end date have only dates then it comes as date from to in excel.
If A1 contains the starting date, and A2 is the enddate, then
=TEXT(IF(EDATE($A$1,ROW() - 5) <= $A$2, EDATE($A$1,ROW() - 5), ""), "mmmyy")
does what you ask for. Just apply it for sufficiently many cells.
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
I have a cell with the following content:
01/02/2015
The cell is date formatted.
Then I copy the value and put it in my module class:
Set Line = New CTravelLine
Line.Date= Cells(1, 8).value
Everything works fine until the moment I put this value in another cell:
The value 01/02/2015 becomes 02/01/2015.
I am using this format (dd/mm/yyyy). I have the impression that when the days are numerically lower than the month, the 2 values are reversed. The values are reversed whatever the method I tried:
Method 1:
Dim WrdArray() As String, datetest As String
WrdArray() = Split(travelLine.Date, "/")
datetest= WrdArray(0) & "/" & WrdArray(1) & "/" & WrdArray(2)
Cells(5, 5) = datetest
Method 2:
Cells(5, 5) = travelLine.Date
I don't understand how I can solve this problem.
This might have happened due to 'Regional formatting problem'.
Excel has a habit of forcing the American date format (mm/dd/yyyy) when the dates have been imported from another data source. So, if the day in your date happens to be 1 - 12, then Excel will switch the date to mm/dd/yyyy.
When dates are imported from a text file, there is an option in the VBA code to apply regional format which corrects this problem.
OR
Change number format of date column in excelsheet from 'date' format category to 'text'; save it.
(After Saving run the VBA Code if you have any. Now check whether the date format is 'text' or changed back to 'date'.)
If it has changed back to 'date' try to fix it as 'text'
If it's 'text'; Correct the erroneous date cells and save the excel sheet. This will make dates not to change automatically to American Format.
Long story short, I had a similar problem where the dates are working just fine in some cells but keep flipping in others regardless if I copy paste or enter manually, I did the whole data text to column and cell formatting solutions and all of that didn't work.
The solution actually is not in excel, it's in the region and language setting.
To have the dates display as MM/DD/YYYY in the formats tab change the format to US.
To have the dates display as DD/MM/YYYY in the formats tab change the format to UK.
I had the same issue as you .
Let me explain what I want to do :
I have a csv file with some date.
I copy a range of my sheet in variable table. The range contain some columns with dates.
I make some manipulations on my table (very basic ones)
I transpose my variable table (since only the last dimension of a variable table can be increase)
I put my variable table on a new sheet.
What I found:
There is no date issue after executing step 1-4. The date issue shows up when writing on the sheet...
Considering what Avidan said on the post of Feb 24 '15 at 13:36, I guess it is excel which forces the American format mm/dd/yyyy... So I just change the date format at the very beginning of my program :
Before starting any manipulation date:
do
.Cells("where my date is") = Format(.Cells("where my date is"), "mm dd yy")
execute your program
write the table on a sheet
back up to the date format you like directly on the sheet
Just use:
Line.Date = cDate(Cells(1, 8).value2)
I'm attempting to create a date range in a merged cell with the following:
=(A1-1) & "-" & (A1-6)
So I'm referencing a date that I have in a cell (A1) and subtracting a few days from it. Once I try to do this it returns the following:
41914-41909
I'd like to be able to keep the dates in the following format:
1/1/2014 - 1/7/2014
How would I do this?
You just need to convert the dates to text before trying to concatenate them as a string.
=TEXT(A1-1, "mm/dd/yyyy") & " - " & TEXT(A1-6, "mm/dd/yyyy")
Here's a similar question on StackOverflow: Convert date field into text in Excel. It gives a little more detail if you have any problems.
I have a spreadsheet with a column formatted as:
Category: Date
Type: *dd/mm/yyyy
Location: UK
When I read the data in this column via VBA, it reads in the format mm/dd/yyyy.
For example, 10/06/2014 (10 June 2014) is reading 06/10/2014 (06 Oct 2014).
My code: sDate = SourceSheet.Range("AB" & CurRow.Row).Value
I have this issue with my forms too and the best method for me is to format the textbox like this:
sDate = format(SourceSheet.Range("AB" & CurRow.Row).Value, "mm/dd/yyyy")
Even though the date format is wrong in VBA, it seems to work the right way round in Excel. It's weird, I can't explain why it happens, but this fixes it for me. Whenever I go from VBA to Excel, I almost always find this issue if the value is stored as a date.
Consider:
Sub luxation()
Dim sDate As Date, CurRow As Range
Set SourceSheet = ActiveSheet
Set CurRow = Range("A1")
ary = Split(SourceSheet.Range("AB" & CurRow.Row).Text, "/")
sDate = DateSerial(ary(2), ary(1), ary(0))
MsgBox Format(sDate, "dd mmmm yyyy")
End Sub
This question of mine - .NumberFormat sometimes returns the wrong value with dates and times - gives some background which may help.
I first encountered this VBA bug many years ago and it is worse than it seems. I noticed that many - but not all - dates in a worksheet that I had been updating for a year were wrong. It took me a long time to diagnose the problem. Those dates that could be interpreted as middle endian dates had been corrupted but those that could not be interpreted as middle endian dates were unchanged. So 12/06/2014 will become 6 December but 13/06/2014 will remain 13 June. If 13/06/2014 had been rejected as an invalid date or left as a string, I would have spotted the error immediately. The dual interpretation so every date was imported as a date - the wrong date but still a date - ensured I did not notice until much later maximising the cost of correcting for the bug.
Excel holds dates and times as numbers. "17 June 2014" is held as 41807 and "1 January 1900" is held as 1. In both cases, the value is the number of days since 31 December 1899. Times as held as a fraction:
number of seconds since midnight
--------------------------------
seconds in a day
So 06:00, 12:00 and 18:00 are held as 0.25, 0.5 and 0.75.
This bug is encountered when the transfer of a date involves a conversion to and from string format. I have not discovered a single case in which the conversion from date to string has been wrong. It is the conversion from string to date that hits this bug.
I can see that SilverShotBee's solution will avoid the bug but it would not appeal to me. I no longer use any ambiguous dates ever.
One choice is to transfer the value as a number. If cell A3 contains the date and time "17 June 2014 9:00" then CDbl(Range("A3").Value) returns 41807.375. When you store this number in a cell you will need to set the cell's NumberFormat to the date format of your choice but that might be a good thing.
If I were going to use middle endian dates, I would be explicit. #13/06/2014# is always interpreted as middle endian.
I prefer unambiguous strings. "2014-06-13" or "13 June 2014" are not misinterpreted by VBA or by a human reader.
Have just come up against this issue! Reading records from a .csv and storing in an .xls
I found the following sequence works to overcome the misinterpreted dates:
Read the date field from the .csv file
Store it into a cell in the .xls file
Read it back into vba
Store into its required destination in the .xls
Date is in original format
I found this issue to be incredibly complex and was trying to keep it as simple as possible but have indeed left a few vital details out! Apologies. Here is a fuller version of what I found:
First of all I should explain I was reading dates (and other fields) from a .csv and storing back into an .xls
I am on Office 2002 running on Windows/7
Using 2 example dates: 27/4/2015 and 7/5/2015 in dd/mm/yyyy string format (from the csv)
What I found was:
Reading the 27/4/2015 text date field from csv into a variable dimensioned as STRING and storing into an xls field in dd/mm/yyyy DATE format produces a cell that reads 27/4/2015 but converting it into a cell formatted as Number also produces 27/4/2015. 7/5/2015 on the other hand produces a string that reads 7/5/2015 and converting it into a cell formatted as Number produces 42131.
Reading the 27/4/2015 text date field from csv into an undimensioned variable and storing into an xls field in dd/mm/yyyy DATE format produces a cell that reads 27/4/2015 but converting it into a cell formatted as Number also produces 27/4/2015 while 7/5/2015 reads 5/7/2015 and converting it into a cell formatted as Number produces 42190.
Reading the 27/4/2015 text date field from csv into a variable dimensioned as DATE and storing into an xls field in dd/mm/yyyy DATE format produces a cell that reads 27/4/2015 and converting it into a cell formatted as Number produces 42121. 7/5/2015 on the other hand produces a string that reads 5/7/2015 and converting it into a cell formatted as Number produces 42190.
The first 3 scenarios above therefore do not produce the desired results for all date specifications.
To fix this I do the following:
Input_Workbook.Activate
ilr = Range("A5000").End(xlDown).End(xlDown).End(xlDown).End(xlUp).Row
For i = 1 To ilr
Input_Workbook.Activate
If IsDate(Cells(i, 1).Value) Then
d1 = Cells(i, 1).Value
d1 = Replace(d1, "/", "-")
ThisWorkbook.Activate
Cells(14, 5).Value = d1
d1 = Cells(14, 5).Value
If VarType(d1) = vbString Then
d1 = CDate(d1)
End If
Cells(i, 1).Value = d1
End If
Next
The cell used to store the date initially is formatted GENERAL and the ultimate target cells is formatted as DATE (dd/mm/yyyy).
I don't have enough brain cells left to fully explain what happens to the dates during this process but it works for me and of course the choice of target cells is completely random in the above code block.
The problem was VBA was opening the csv with the reverse dates for single digit days.
This way of opening the workbook worked the same as when I did it manually so had the correct dates in dd/mm/yyyy format. Then copied across correctly:
Workbooks.OpenText FileName:=fpathO, datatype:=xlDelimited, comma:=True, local:=True