I have the following code to remove some "garbage" from cell contents.
Sub RemoveChar()
With Cells
Cells.NumberFormat = "#" ' setting format as text
Cells.Replace What:="#", Replacement:=""
End With
End Sub
The sheet contains values like #123451234512345 and when I remove the # the value changes to look like this 1.23451E+14 even when above I changed the format to text before the replacement.
How can I remove the character # and after remove it say to Excel to show the value as 123451234512345 and not as 1.23451E+14?
Thank in advance.
Try this : (Change 'ActiveCell' to your needs)
ActiveCell.NumberFormat = "0"
Related
I export Excel files from a website. The exported files have number values with a currency sign before them in a number of columns although the number format of the cells is "General".
I want to remove the currency sign.
Another tricky thing is the headers of these columns and even the column indexes are not constant.
Is there a way to select the cells/columns and remove the currency sign that can work on any Excel file I am working on? Is there an easier solution?
Example of the problem:
try
Range("K2:M6").Replace What:="$", Replacement:=""
or if your prefer selection before:
Selection.Replace What:="$", Replacement:=""
(it's just as quick by hand: just press ctrl+h)
Sub RemoveDollarSigns()
Dim cell As Range
For Each cell in Selection
cell.Value = val(replace(replace(cell.Value, "$", ""), ",", "")
Next cell
End Sub
I'm working on a macro to add separators and an apostrophe at the beginning of a number:
Sub Apostrofe()
Selection.NumberFormat = "#,##0"
For Each cell In Selection
cell.Value = "'" & cell.Value
Next cell
End Sub
Output for 123456789 is 123.456.789 but when it adds the apostrophe at the beginning, the format is lost. On the formula bar i see '123456789 but the separators won't appear unless i remove the apostrophe from the number.
I tried to concat the number in parts while manually adding the ' and . but the result is the same.
Adding the apostrophe in the Format of the code added it on the cell but it wouldn't appear on the formula bar.
If i manually write the apostrophe + number with separators it works but i receive hundreds of numbers which must be formatted this way.
How can i modify the macro to do what i need?
If you use an apostrophe the value will be converted to text.
It seems that in your example you do not change the value itself (to the text equivalent containing the formated number), you just apply the format to the cell and then overwrite it with '.
I think you should try following code:
cell.NumberFormat = "#" ' set cell format to text
cell.Value = Format(cell.Value, "#,##0") ' Format() returns formated number as a string
i wrote a code in VBA to change values in a range from text into general (number). it works fine, however there is one issue. when i checked all the column values, only the string with an integer value ("2345") is changed to integer (2345) but the strings with float values ("123.456") didn't change at all, the type changed into General but the value stay the same.
here is the vba code
If (ws.Name = "TB") Then
shTB.Activate
[A:A].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
results as the table below, still the float values didn't change at all. as you can see only row 119 changed into Number or integer.Any suggestion or help is appreciated highly!
It is clear from your results that there is a mis-match between the text format of your data and your Locale settings.
You data appears to have come from a source in which the comma is the decimal separator rather than the period. Try:
Sub dural()
With Range("A:A")
.NumberFormat = "General"
.Replace ",", "."
.Value = .Value
End With
End Sub
if that "." stands for thousands separator then just remove it
With shTB.Range("A:A")
.Replace what:=".", replacement:=vbNullString
.NumberFormat = "General"
End With
BTW, as you see you don't need to select anything: just act on its direct reference
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.
I have few numeric columns which I got from a website and copied directly into Excel.
In those columns there is a SINGLE Leading space at the beginning of each number in the cell of the entire column.
try provide vba or any excel formula
This should work.
Sub RemoveSpaces()
Selection.Replace " ", ""
End Sub
Did you try using the Trim() worksheet function?