Excel Changes number value automatically - excel

I´m trying to remove the dollar sign in a column which should only include numbers. Therefore I tried to to use simply change the cell format to number but nothing changed.
Now I copied the values inside a text editor and removed the dollar signs. After inserting excel automaticallly changes some values to different numbers.
For Exaxmple it changed 8.59 to 21763,00. When I change the cell format to standard then it displays me something like 28 Jan except 8.59.
In this picture I tried to illustrate my problem with the different columns. Sold Price in Thousands is the original column which I liked to change.

Select the cells you wish to fix and run this short macro:
Sub FixData()
Dim r As Range, s As String
For Each r In Intersect(Selection, ActiveSheet.UsedRange)
s = r.Text
If Left(s, 1) = "$" Then
r.Clear
r.Value = Mid(s, 2)
r.NumberFormat = "0.00"
End If
Next r
End Sub

this is a known issue and the only work around that you can use is the following:
Copy the correct values in notepad.
From notepad make a Find and Replace in order to remove the $ sign.
Select a blank column in excel and set its format to TEXT.
Only now you can copy back the values from notepad to the new TEXT column.
This should fix your issue.

Related

VBA delete Formula Sign from Excel

I am doing a validation at work and already did a lot of work, but now I am facing a problem. I need to delete the ' sign to clear unwanted Excel Formulas. I tried with Replace but it did not remove the '. Does anyone know how to delete it from a cell?
The apostrophe is kept hidden, not as part of the value so you cannot check for it via value or text or formula property. What you need to check for is the PrefixCharacter. Alas, that is read-only.
Only way around this: clear the cell and re-set it.
Short example that you should be able to implement:
Dim txt As String, r As Range
Set r = Cells(2, 1) 'contains apostrophe
If r.PrefixCharacter = "'" Then
txt = r.Value
r.Clear
r.Value = txt
End If
Put this in an appropriate loop for your cell contents and you should be set.

Copy/paste dates exactly as displayed? Not necessarily the format, but as they're displayed visually

I can't seem to figure out how to overwrite my date with what's being displayed.
For example, what's being displayed is 06/01/2016, but the actual data behind that is showing 01/06/2016. The date I want to have in there is June 1, 2016 (which is what's displayed). Changing the format of the cell doesn't help, because I'm doing some formulas with the Dates later, so actually need to have Excel have the correct date.
I have about 10,000 of such dates, where the displayed date is exactly what I want to have, but the actual formula "reversed".
Essentially, I would love to just copy that column (or run a sub) that puts what the cell literally displays into the cell.
What sort of works is doing this, but it only works on those cells with "backwards" dates...essentially passing the day as a month, and month as a day.
=DATE(YEAR(C1),DAY(C1),MONTH(C1)).
It "falls apart" though when the date is actually correct and I don't want it to change:
So, what'd be best is to just literally overwrite the cell with what's displayed.
Or, what would an IF statement be that I could use to somehow check if the date displayed is what I want, and if so keep that, otherwise use that =DATE(YEAR(),DAY(),MONTH()) "trick"?
I also tried =IF(DAY(C2)>12,DATE(YEAR(C2),DAY(C2),MONTH(C2)),C2) but that doesn't work either because it returns June 01 2016 for both 01/06/2016 and 06/01/2016.
I may be overlooking something simple, I've been staring at this for an hour or so...Any thoughts/ideas are appreciated!
Say wee have dates in column C from C1 through C100 that appear to be correct. (so if you see 06/12/2017 you want it to be June 12th and not December 6th)
Try this short macro:
Sub dateFixer()
Dim ary(1 To 100) As String
Dim rng As Range, r As Range
Dim i As Long
Set rng = Range("C1:C100")
i = 1
For Each r In rng
ary(i) = r.Text
i = i + 1
Next r
rng.Clear
rng.NumberFormat = "mm/dd/yyyy"
i = 1
For Each r In rng
r.Value = ary(i)
i = i + 1
Next r
End Sub
A really clean solution is to use the CELL() function.
=CELL("format",A1) will return "D1" for cells formatted as d/m/y, and "D4" for cells formatted m/d/y. So, with this you can conditionally flip month and day:
=IF(CELL("format",A2)="D1",DATE(YEAR(A2),DAY(A2),MONTH(A2)),A2)
The Text property of a range returns the display text: "what the cell literally displays."
Note that literal is the right word - for example, if your column is too narrow, Text will return the displayed ##### characters instead of any useful value.
The Text property will not return an array, so you'll have to loop through your range and read/write individual cells with something like this:
For Each c in rng
c.Value = c.Text
Next c
Incidentally, the documentation on this property is almost nonexistent. This blog post goes into a more detailed review of the property and how it relates to Value and Value2.

How to eliminate #NAME? error Excel for a long list of IDs starting with =

I have a long list of ids in excel. (a couple 100k rows) and every so often one of the ids will start with an equals. This causes excel to think it's a formula and gives me the #NAME? error. I know about adding a quote to the beginning of a forumla to make it text, but is there a faster way to do this since I have so many rows?
I also tried converting the cells to text and concatenating a ' at the beginning, but it still comes out as a #name? error.
Thank you!
=IFNA(FORMULATEXT(A1),A1) will do it, assuming A1 contains the datum.
FORMULATEXT(A1) will "undo" the conversion of =Something back to the original text, but returns #N/A if the cell doesn't contain a formula. The latter is circumvented with IFNA.
(Obviously you can copy and paste-special values once you've added what presumably is an extra column in your workbook to deal with this calculation.)
With data like:
running this small macro:
Sub FixEquals()
Dim r As Range, rng As Range, s As String
Set rng = Range("D:D").Cells.SpecialCells(xlCellTypeFormulas)
For Each r In rng
s = r.Formula
r.Clear
r.NumberFormat = "#"
r.Value = s
Next r
End Sub
will produce:

Search and Replace random cell text by first :

I have a slew of rows (100K+) to search and modify the contents.
For example the cells contain similiar text as DGC9610411:DB:10:82
All of this text can change per row except that fact that the : means something to me.
In this and every other row, i need to remove the first : and all the text after so that the cell would look like this DGC9610411
Next I will be adding the contents of another cell to the end. I think that will be an easy step and I could most likely figure that out without much effort.
I have this code in a while loop for each row. so the code is looking at one row at a time.
I have searched but everyone seems to have a different set of needs.
Just use Find and Replace, no need for vba or formulas.
Select the column containing the data that you need to modify
Press Ctrlh to open the Find and Replace dialog.
In the "Find what:" field, type :*
In the "Replace with:" field, leave it blank
Click Replace All
EDIT: If it has to be VBA, this simple macro will accomplish the same thing. Be sure to change the A to your actual column letter.
Sub tgr()
Columns("A").Replace ":*", ""
End Sub
EDIT: Upon request I am adding a formula solution. In cell B1 and copied down:
=LEFT(A1,FIND(":",A1&":")-1)
Try this small macro:
Sub colonoscopy()
Dim c As String
Dim r As Range, I As Long
For Each r In ActiveSheet.UsedRange
v = r.Value
I = InStr(1, v, ":")
If I > 0 Then
r.Value = Mid(v, 1, I - 1)
End If
Next r
End Sub

formatting excel cell

I am in trouble. I have thousands of customer data having name, address, zip. I have data in excel and the problem is in zip code. The Zip should be 5 character long and if it is not, we have to add zero at front to make equal to five character. Since there are thousands of data and it is not feasible to change it one by one, can somebody suggest me to format the column of zip so that it could be 5 character long.
You can use the custom format and use the type : 00000
Write the following formula in front of that zip column:
=IF(LEN(A1)>5,"Invalid",RIGHT("00000" & A1,5))
Just replace the cell reference name A1 to the first cell of zip code column and then drag the cell down. Now all those zip codes that are less than five characters will be filled with leading zeros. Also It will show Invalid in cells which have a zip code of more than 5 characters length.
Apply this formula to the cell values, i.e. for A1 in this example:
=TEXT(A1,"0000#")
Copy/paste the formatted values (as text) into the desired column if you need them in a specific column.
Simply mark the whole column.
Than right click to get into the "format cells" settings.
There you go to "special format" and there you should mark "ZipCode".
Here is a picture of the options. It is in german, so I hope you will find it anyway ;)
Quick macro to convert the range to Text format, and append leading zeros if the cell is less than 5 characters.
Sub AddLeadingZeros()
Dim rng As Range: Set rng = Range("A1:A10") '<modify as needed, the cells containing ZIP'
Dim cl As Range
rng.NumberFormat = "#"
For Each cl In rng
If Len(cl.Value) < 5 Then
Do
cl.Value = "0" & cl.Value
Loop While Len(cl.Value) < 5
End If
Next
End Sub
select your column
Format-> cell
Special format-> zip code
make sure you save the changes... it shouldn't remove zero...
or try a "user defined" format... but I never tried it.

Resources