I have a column V which contains date in dd/mm/yyyy format. However, system format shows it as mm/dd/yyyy. I want to convert the date format from dd/mm/yyyy to dd-mmm-yyyy.
ThisWorkbook.Sheets(1).Range("W" & i).NumberFormat = "dd-mmm-yyyy"
When I convert it using the above code, it changes the value from 08/02/2017 to 02-Aug-2017. It should change to 08-Feb-2017. Please assist.
There is nothing wrong with your code. You have entered "08/02/2017" - This is interpreted as input in the format of "mm/dd/yyyy". As a result, the cell is showing "02-Aug-2017".
You must change your system date format to read the input (08/02/2017) as date value with format "dd/mm/yyyy". After that your cell will show you the date in desired format & value (08-Feb-2017).
To change the system date format, go to Control Panel > Region & Language. Your current format must be "English (United States)" [or something with mm/dd/yyyy date format]. Change it to "English (United Kingdom)".
This should take care of your problem.
Start from the very beginning and try to simplify everything. 8.Feb.2017 is the 42774th day in the Excel system. Thus, in an empty sheet run the following and check the results:
Option Explicit
Public Sub TestMe()
[a1] = 42774 '08-Feb-17
[a2] = [a1]
[a3] = [a1]
[a4] = [a1]
[a2].NumberFormat = "dd-mmm-yyyy"
[a3].NumberFormat = "mmm-dd-yyyy"
[a4].NumberFormat = "dd-mm-yyyy"
End Sub
In Excel, day 1 is 1-Jan-1900.
In VBA day 1 is 31-Dec-1899.
Related
I am struggling with formatting dates in Excel and am looking for some help.
I have two different date formats in the same column:
MM/DD/YYYY
DD/MM/YY
I am struggling to change the DD/MM/YY to MM/DD/YYYY. I have tried so many things and can't get it to work.
Public Function dateguesser(inDate As String) As Date
dateguesser = CDate(Split(Split(inDate, " ")(0), ",")(0))
End Function
Things I have tried:
This solution ends up thinking that the DD in the DD/MM/YY format is the year because the year is not stored as four digits.
Tried to change MM/DD/YYYY to DD/MM/YY and then convert all of them to MM/DD/YYYY.
Text to columns
If you put this formula starting in B3, I think it should work for all values?
=IF(ISNUMBER(A3),A3,DATE(2000+RIGHT(A3,2),MID(A3,4,2)+0,LEFT(A3,2)+0))
You could also probably modify your custom function to something like this:
Public Function dateguesser(inDate As Variant) As Date
If IsNumeric(inDate) Then
dateguesser = inDate
Else
dateguesser = CDate(Split(Split(inDate, " ")(0), ",")(0))
End If
End Function
I'd add another column to convert to epoch and back to new date format. https://exceljet.net/formula/convert-unix-time-stamp-to-excel-date could help
Assuming the input is in A10:A11, I made for B10:
=((A10-DATE(1970,1,1))*86400/86400)+DATE(1970,1,1)
and applied new date format to B:B.
I want to fill a userform textbox with a date taken from a cell. The date in the cell is in 'dd/MM/yyyy' but the date drawn from this cell into the textbox comes always as 'MM/dd/yyyy'. Any suggestions would be highly appreciated as I have been wasting tons of hours on that issue.
Text_checkin = DateValue(Sheets("Data").Range("Data_St").Offset(Trgt, 3).Value)
Computer's system date setting decides how DateValue outputs a date. You may want to read about DATEVALUE function
To show the result in a specific format, use the Format Function
Is this what you are trying?
Text_checkin = Format(DateValue(Sheets("Data").Range("Data_St").Offset(Trgt, 3).Value), "DD/MM/YYYY")
Thanks,
It seems that it works for me:
Text_checkin = DateValue(Sheets("Data").Range("Data_St").Offset(Trgt, 3).Text)
The System's regional date settings was set to English (UK) dd/MM/yyyy all the time.
I have been trying to solve an issue I have with copying dates from Word document to Excel using VBA.
My issue is the format. If I put dd/MM/yyyy it will change during the process to MM/dd/yyyy and I cannot solve the issue.
Basically for copying I am using this line:
Sheets("wImp").Range("AA" & i) = wdDoc.ContentControls(1).Range.Text
However during the process the data is modified.
I originally used LegacyForms but it did work and I hoped Date Picker would help but it does not. And in a case the date is impossible to switch like 21/12/2017 it will stay as it should. But when it can be switched like 1/5/2017 it will be switched to 5/1/2017.
!!! Nevertheless the cell format stays dd/MM/yyyy for all the dates, but the months and days position is switched.
See below for what I have as input in Word and the output in Excel.
Word:
Excel:
If you want to use the continental date format "dd/mm/yyyy" in your sheet you must use Format() function. So, try this:
Sheets("wImp").Range("AA" & i) = Format(wdDoc.ContentControls(1).Range.Text, "dd/mm/yyyy")
Sheets("wImp").Range("AA" & i).NumberFormat = "dd/mm/yyyy"
Oficial documentation of Format() function
Sub test()
ActiveSheet.Range("A1").NumberFormat = "#"
ActiveSheet.Range("A1").Value = "01/05/2017"
ActiveSheet.Range("A2").NumberFormat = "m/d/yyyy"
ActiveSheet.Range("A2").Value = "01/05/2017"
ActiveSheet.Range("A3").NumberFormat = "yyyy/m/d;#"
ActiveSheet.Range("A3").Value = "01/05/2017"
End Sub
Try something like this.
Excel will convert the data during copy.
So, change "NumberFormat" before copy.
Assigning a string to the cell makes Excel think that the string should be interpreted in a "mm/dd/yyyy" format irrespective of your locale (and irrespective of the cell's date format).
So convert the string to a Date using the CDate function (which will be done using your system date/time settings) before assigning the value to the cell.
Sheets("wImp").Range("AA" & i) = CDate(wdDoc.ContentControls(1).Range.Text)
That date can then be displayed in whatever date format you want to use for the cell.
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 received the following task:
turn "dd/mm/yyyy hh:mm:ss" time to "yyyy.mm.dd hh:mm" format.
I was hoping that a simple Format(activecell.value, "yyyy.mm.dd hh:mm") would help, but.
In some cases it works. However, in some special cases, it doesn't.
When the day is between 01-12 (interchangeable with months!), then it messes up the days with months. (Probably because of US style date format.)
For example, original text string:
04/06/2013 09:00:00
Expected:
2013.06.04 09:00
Result:
2013.04.06 09:00 (day and month have been swapped)
I tried to overcome it by formatting the input, I gave it a "dd/mm/yyyy hh:mm:ss" custom format. Didn't help, it still swaps the day and the month.
So I have no ideas anymore, but regular expressions. Date format is always the same so just moving the parts of the string would be okay.
However, I don't know how that works and google searching brings up confusing results.
Can anyone help me?
Of course if there is a more elegant way to do it, that's also welcome.
Try using this Custom Format:
yyyy/mm/dd h:mm;#
To enter it, right click on the cell and Choose Format Cell:
Then under the Number Tab Select Custom from the listbox. And enter the provided format in the Type: textbox.
If that doesn't work. Are you *****Absolutely***** Positive that 04/06/2013 09:00:00 IS April 6, 2013 and NOT June 4, 2013??
If this still doesn't work and you have verified that dates are correct.
Sub ChangeDateFormat()
Application.ScreenUpdating = False
Dim CurrentCell As Range
Dim LastRow As Long
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
LastRow = Range("A" & Rows.Count).End(xlUp).Row 'Get The Last Row in Column Change A as Needed
For Each CurrentCell In Range("A1:A" & LastRow) ' Loop through all cells. Change Column as needed
If InStr(CurrentCell.Value, "/") <> 0 Then 'To make sure only convert non converted ones
RegEx.Pattern = "(\d{2})/(\d{2})/(\d{4}) (\d{2}):(\d{2}):(\d{2})" ' Seperate all parts of imported Data into groups
CurrentCell.Value = RegEx.Replace(CurrentCell.Value, "$3.$2.$1 $4:$5") ' Change order of groups and place into cell.
End If
Next
Application.ScreenUpdating = True
End Sub
*****NOTE: ***** This will only work if ALL date values are of dd/mm/yyyy hh:mm:ss if they are not you will have to add some Error Handling and possibly slightly modify the code as it WILL cause problems.
This will also work on dates that are inside of other text. If the value of A1 is
This is a test 04/06/2013 09:00:00 Lets see what happens
Then the new value will be
This is a test 2013.06.04 09:00 Lets see what happens
its simple. . . just follow the steps
select the entire column or even required cells
press right click and select format cell option, a tab window will open
from number tab select date , from locale drop down menu select English (South Africa)
from given options in type select your desired reverse format . . . and you done :)
If the month and day are in the wrong place, you can use "DATE(YEAR(A1),DAY(A1),MONTH(A1))", provided it's in date format, and "DATE(MID(A1,7,4),LEFT(A1,2),MID(A1,4,2))" if it is in text format