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
Related
I am reading some cells of type accounting into a Variant variable and then writing the variable in a new workbook. When I do this, the number formatting gets all mixed up.
I first read the cell from the first workbook with value 69,77 € and cell format Accounting:
Dim tiendas As Variant
x=.Cells(1,1)
Then I create a new workbook and write the content of the variable into a cell:
wb.Worksheets(1).Cells(1, 1) = x
If I don´t do anything else, the copied value looks like this 697.715 and has a cell format of Number.
Then I try changing the cell format before writing the value, i.e.:
wb.Worksheets(1).Range("A1:A1").Style = "Currency"
wb.Worksheets(1).Cells(1, 1) = x
As a result I get € 697.715,00 and cell type Custom.
I have tried inverting the order, i.e. first writing the value and then changing the style to "Currency". This results in exactly the same result.
I have also tried changing the format in the different way, specifically by using what appears in the custom formatting in Excel (and doubling the quotes inside the string):
wb.Worksheets(1).Range("A1:A1").NumberForma="_-* #.##0,00 €_-;-* #.##0,00 €_-;_-* "" - ""?? €_-;_-#_-"
wb.Worksheets(1).Cells(1, 1) = x
The result in this case is 697715,000 € and cell format Custom.
It seems that the original number format is not being read properly but I don´t know how to fix this.
Any ideas are very mucho welcome.
Thanks in advance!
NOTE: My number setting in Windows are "," as a decimal symbol and "." as thousands separator.
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 cells containing a simple concatenation function with D24 as the previous year (e.g. 15) and a custom format (MMM JJ)
CONCATENATE("Apr ",$D$24)
When I am copying and pasting these cells with a VBA then "Apr 15" becomes "15.04.16" and because of the formatting "Apr 16"
Selection.Value = Selection.Value
Was is the reason behind this? Is there another solution than just changing the format to text?
Excel will generally try to convert anything that looks like a date into a real date (serial number where 1 = 1 Jan 1900). One way to avoid that, and remove the formula as you are doing above, would be to pre-format as text. So:
With Selection
.NumberFormat = "#"
.Value = .Text
End With
might do what you want.
There are other ways, but if you don't change the cell format to text, or prefix the entry with a single quote ', any subsequent editing of that cell, even inadvertent selection, raises the risk of converting it to the real date.
That depends on what you want in your cell. Dou you want a string or a date?
If you want a string:
either format as text or
add a ': CONCATENATE("'Apr ",$D$24)
if you want a date:
use the following formula instead of concatenate: =DATE($D$24,4,1)
If you simply Copy Paste it, only the Value is pasted not the formatting (if I remember right)
Try to avoid using Selection instead use Range.
And use Range.Copy and Range.PasteSpecial Paste:=xlPasteFormats so your formatting is pasted with the values.
This problem seems very simple, yet I just can not find the solution (I am already loosing my mind about it :) )
OK, so I just want to put a certain value into an excel cell, using vba code, just as simple as this:
Cells(1,1).Value2 = "123,456"
The problem: this is a string (intentionally), but excel always convert it to number, and put that number into the cell, instead of the string that I wanted.
How can I force excel not to convert it, and just put into the cell exactly what I want (the string)??
Thanks,
Cells(1,1).Value2 = "'123,456"
note the single apostrophe before the number - this will signal to excel that whatever follows has to be interpreted as text.
Indeed, just as commented by Tim Williams, the way to make it work is pre-formatting as text. Thus, to do it all via VBA, just do that:
Cells(1, 1).NumberFormat = "#"
Cells(1, 1).Value = "1234,56"
This is probably too late, but I had a similar problem with dates that I wanted entered into cells from a text variable. Inevitably, it converted my variable text value to a date. What I finally had to do was concatentate a ' to the string variable and then put it in the cell like this:
prvt_rng_WrkSht.Cells(prvt_rng_WrkSht.Rows.Count, cnst_int_Col_Start_Date).Formula = "'" & _
param_cls_shift.Start_Date (string property of my class)
I have a cell with the contents of 41316. I have it formatted as 20130211.
I need another cell to reference that value, but in the cell have it showing as 20130211 not 41316 formatted.
This maybe an easy one, but for some reason it has me running in circles.
Thanks in advance for all responses.
Excel by default copies the format from a cell formatted as a date to a cell which references it. It sounds in your case that Excel hasn't done that for you, so you just need to format the new cell with your special format : paintbrush tool or Edit..Copy, Edit..Past Special..Formats or Format..Number..Custom and select your format, which will be at the bottom of a long list.
If a string is ok instead of a number, you can decompose that date in parts and concatenate:
Being A1 the cell containing the value:
= Year(A1) & "/" & Month(A1) & "/" & Day(A1)
The "&" symbol concatenates text. The slashes are optional if you want them separated by slashes.
if your cell referencing 20130211 is 'A1' put =TEXT(A1,"####") in your calculation cell. If you do it this way then it will still read as a number and not a string