Copying date Word to Excel - wrong format - excel

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.

Related

excel vba change format of date - numbers

Excel changes the following values automatically to a number, I guess because he considers them as a date:
2/2/1 becomes 36527
4/2/1 becomes 36926
I have a column with a combination of different formats now:
2/1/
3/1/
8/7/
36527
1/0/0
36926
Which VBA code can I use to convert the numbers back to their original format? The other values should stay the same.
I know the cDate function, but I guess it's not useful here?
I have already this in my VBA code for pasting values
ActiveWorkbook.Sheets("Import").Columns("A:AH").NumberFormat = "#"
ActiveWorkbook.Sheets("Import").Range("A1").Select
ActiveSheet.Paste
You can't change them back. Once Excel converts them, the original value is gone. Before you input the value, you can prepend an apostrophe to force it to text.
ActiveCell.Value = "'" & sMyValue
or as #Scott Craner commented, you can format the cell as text
ActiveCell.NumberFormat = "#"
ActiveCell.Value = sMyValue

data format gets converted unexpectedly from dd/mm/yyyy to mm/dd/yyyy

I have a userForm to collect some data to be inserted in a excel worksheet.
One of the field is a date and for my locale dates are always dd/mm/yyyy.
I put a date (i.e. 13/08/2018) in the form and debugging the code I see:
data_mov = new_mov.data.Value
the value is taken from the form as I wrote it. So it is still 13/08/2018. This evaluates to 13/08/2018 = 13/08/2018
Then I put the value in a specific cell. Debugging i see that
Range("A" & riga).Value = data_mov
evaluates to 08/13/2018 = 13/08/2018
So the cell stores 08/02/2018. Why does it happen? I have no intermediate code that can manipulate the date. How can I prevent this to happen?
Try adding this line to your code:
Range("A" & riga).NumberFormat = "dd/mm/yyyy;#"
It will apply desired formatting to your cell.
Alternatively you can use:
Cells(riga, 1).NumberFormat = "dd/mm/yyyy;#"

VBA to change the format of date

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.

Converting a Date in Text to a Date Field in Excel, i.e. 6/2/2017 9:24 AM EDT

I'm not new to Excel and or Access, but have never come across this before. A report was sent to me where a date field is being stored as text, but is outputted as 6/2/2017 9:24 AM EDT. I'm trying to convert this column into a date field so that I can do calculations off of it in Access. I wold love to do this in Access, but I think it might be easier to do in Excel. I've tried doing a text to column and then formatting the Cell as Date, i.e. 3/14/2001, but to no avail. I thought about doing a Left, but the date could be 6/2/2017 or 12/2/2017. Any help would be greatly appreciated.
Use this:
=--LEFT(A1,LEN(A1)-3)
Then format the cell as you wish.
Note, this only works if your local date format is d/m/y not m/d/y if that is the case you will need to parse the data bit by bit.
You can use substitue. Like this:
=SUBSTITUTE(A1;"EDT";"")
If you are saving it as a CSV or doing any copy/paste and want to make sure Excel doesn't convert it into a date type at reimport, you can add an apostrophe before the value which tells Excel to explicitly ignore what follows it. Here is one way to do it automatically using a macro.
Format the column/rows as text and add this to the workbook macros:
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If IsDate(.Value) Then
Application.EnableEvents = False
.Value = "'" & .Value
Application.EnableEvents = True
End If
End With
End Sub

Problem with CDate function in VBA

I'm using CDate to convert a particular date formatted as string to the Excel Date type. I wrote a small UDF for this conversion; however when I run the function as a in-cell function, it returns a number and the calling cells default format is set to Number type. When I manually convert it to Date type, Excel will display the correct date entry.
So, I need to know whether there is a way to set the default format of the calling cell to Date type through my Date conversion macro. Otherwise, the user has to manually change the format of each of cell.
e.g
Function Test()
Test = CDate("2010/12/23")
End Function
The calling cell is exposed via Application.ThisCell however your going to have to manually format before/after the call as an Excel UDF cannot modify the physical characteristic of a cell.
Perhaps you can run something after the cells have been entered?
Dim c As range
For Each c In ActiveSheet.UsedRange.Cells
''Case sensitive
If c.Formula = "=test()" Then
c.NumberFormat = "d/mm/yyy"
End If
Next
It sounds like what you want to do is actually a two-part process: convert a value in a cell (or range of cells) to a date and then display it as a date, as opposed to simply converting a text value that looks like a date to a date value.
If that is the case, then I would recommend modifying Remou's suggestion like so (using UsedRange can be problematic on worksheets containing large amounts of data):
Dim c As Range
For Each c In Selection.Cells
c.Value = CDate(c.Value)
c.NumberFormat = "m/d/yyyy"
Next c
The user would then need to select the cells to which the formatting should be applied and run the macro; it sounds as though you do not wish this to happen, but I'm not sure that is possible.
Depending on how you want to use the macro, you can add additional checks: for example, you could apply the formatting only to non-blank cells currently formatted as text (c.Value <> "" and c.NumberFormat = "#").
If you return the data as a string and in a format that Excel automatically converts to a date, you can get the cell to format properly.
Function Test() As String
Test = Format(DateValue("2010/12/23"), "mm/dd/yyyy")
End Function
This works in US Excel, but if you need it to be language agnostic, I think you'll have problems.

Resources