How can I remove the currency symbol from values in selected cells? - excel

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

Related

Azure Data Studio produced Excel spreadsheet displays cell values as text (not the result of the formula's execution)

Original Problem:
Within Azure Data Studio, I have saved the results of a SQL query into Excel spreadsheet. When I bring up the spreadsheet, a cell containing a formula (a URL hyperlink) is showing as text to display as opposed to results of executing the formula (a named visibly clickable hyperlink).
Here's the first three rows of raw data from the spreadsheet:
sourceTableNameA
sourceTableComposedKeyA
sourceTableComposedKeyA2
sourceTableNameB
sourceTableComposedKeyB
FCC_ASR_LI_20210202
=Hyperlink("https://maps.google.com/maps?q=28.537266254426,-97.794735431672","C.2655506")
https://maps.google.com/maps?q=28.537266254426,-97.794735431672
F3GIS_20210209
=Hyperlink("https://maps.google.com/maps?q=28.537266254426,-97.794735431672","{F4C8C17C-0702-4B08-ABC5-7ABF0BD76BAA}")
FCC_ASR_LI_20210202
=Hyperlink("https://maps.google.com/maps?q=29.709928035737,-96.912009716035","C.616145")
https://maps.google.com/maps?q=29.709928035737,-96.912009716035
F3GIS_20210209
=Hyperlink("https://maps.google.com/maps?q=29.709928035737,-96.912009716035","{7F77EAB2-3588-4023-921F-4C009FC91BDC}")
Solution A:
By clicking into the cell (ex: B2 above) and then pressing Return, the cell then turns into the results of the formula.
New Problem:
I have +20K of cells which I need to execute "Solution A". And manually executing "Solution A" +20K times is not an option.
Solution B (barely adequate):
After investing considerable time trying to fix this, I discovered a marginal solution. It is to select the contents of the column containing the formula-as-text, then search/replace the "=" character. While this does in fact convert the text into an actual formula for each cell, it leaves each cell looking like regular text instead of making it have the default appearance of a clickable link. This is causing me to have to then manually underline and select a different color for all of these cells.
Solution C (possible?):
My first thought was this is a job for an Excel Macro in VBA. And this is as close as I was able to get:
Public Sub Convert_To_Hyperlinks()
Dim Cell As Range
For Each Cell In Intersect(Selection, ActiveSheet.UsedRange)
If Cell <> "" Then
If Left(Cell, 1) = "=" Then
Evaluate (Cell)
Else
ActiveSheet.Hyperlinks.Add Cell, Cell.Value
End If
End If
Next
End Sub
This function works exactly as I need for cells (ex: C2 above) that contain a raw URL (i.e. the cell does not start with "=" character) which calls the ActiveSheet.Hyperlinks.Add Cell, Cell.Value for each cell giving the desired result. However, the code that detects if the cell (ex: B2 above) starts with "=" which calls Evaluate (Cell) doesn't work on the selected cells.
What VBA code can I use to replace Evaluate (Cell), if any, which would produce the same effect as Solution A?
In projects I have worked on I have found the following is often sufficient:
Cell.Value = Cell.Value
The beauty is that you can apply to ranges in one line.
Applying to your code with the loop:
Public Sub Convert_To_Hyperlinks()
Dim Cell As Range
For Each Cell In Intersect(Selection, ActiveSheet.UsedRange)
If Cell <> vbNullString Then
If Left$(Cell, 1) = "=" Then
Cell.Value = Cell.Value
Else
ActiveSheet.Hyperlinks.Add Cell, Cell.Value
End If
End If
Next
End Sub

how to remove space between letters in EXCEL?

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?

VBA remove " from cell content in a range

I am new to vba coding (and coding in general) but I have started doing a small macro that transfers values of a csv file into an excel file.
My next step is to remove the quotation marks in each cell. I have tried these lines of codes in my sub:
Dim Table As Range
Dim Cell As Variant
Set Table = Range("A1:currentregion")
For Each Cell In Table
cell.value2 = Replace(cell.value2,""","") *I get a syntax error here*
Next Cell
I know this is very basic but I can't find a solution on the net or using the macro recorder. If anybody could tell me what I am doing wrong and also what code I could use to change the value in a cell that contains a string of numbers into a numeric value?
Thanks!
You don't need to loop through each cell. The Range("A1").CurrentRegion may be operated on as a whole.
With Range("A1").CurrentRegion
.Replace What:="""", Replacement:=vbNullString, LookAt:=xlPart
End With
Note that to look for a single quote (e.g. ") you need 4 quotes in a row. An alternative is Chr(34) as a quote symbol is the 34th ASCII character.
Addendum:
With regard to the second portion of your question, changing text that looks like a number to actual numbers is best done with a quick Text to Columns ► Fixed Width command. Each column would have to be run through individually but this can be accomplished within the .CurrentRegion.
Dim c As Long
With Range("A1").CurrentRegion
.Replace What:="""", Replacement:=vbNullString, LookAt:=xlPart
For c = 1 To .Columns.Count
.Columns(c).NumberFormat = "General"
.Columns(c).TextToColumns Destination:=.Columns(c), _
DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
Next c
End With
There is the question of what number format the columns were in to begin with. If they were Text (e.g. '.NumberFormat = "#"` then removing the quotes is not going to convert them to true numbers. I've added a reversion to the General number format to accommodate this. If you have existing number formats that you wish to keep, there are more precise ways of reverting the number formats of some of the cells.
Cell.Value2 = Replace(Cell.Value2, Chr(34), "")
There always is a workaround, this one is referencing via character code.
You should dim Cell as Range. Also it's not a very good idea to name a variable using a VBA syntax element. Use MyTable and MyCell instead.
And to set the range better use
Set MyTable = [A1]
For Each MyCell In MyTable.CurrentRegion
...

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.

Excel 2007 VBA Code- Splitting Cells

Excel 2007- I have countless old Word Tables that I'd like to put into Excel. I'd like to split the contents of the cell into two cells. Most of the cells have a very similar format (I don't need to split the ones without this format)- Text (Date). I've tried using "LEFT" or "RIGHT" but since the text string and date string are variable lengths there are no good straightforward ways. For example-
Cell A1-
"Market Value (6/16/09)" [or "Addition (12/15/09)", etc.]
I'd like to split the cell into-
Cell A1- "Market Value" and
Cell B1- "6/16/09"
Obviously if it takes the A1 data and puts it into B1/C1 I could care less.
I've seen some other split VBA modules but they don't seem to be doing the trick for me. I've looked for ways to split it using CSV but that doesn't seem to be useful either. So is there a way to use the "(" or ")" as a marker to copy text before or after the "("?
So is there a way to use the "(" or ")" as a marker to copy text before or after the "("?
Yes
Example
Cell A1- "Market Value (6/16/09)"
Sub Sample()
Dim Ar() As String
Ar = Split(Range("A1").Value, "(")
Debug.Print Ar(0) '<~~ This will give Market Value
Debug.Print Ar(1) '<~~ This will give 6/16/09)
'<~~ And the below will give you 6/16/09
Debug.Print Replace(Ar(1), ")", "")
End Sub

Resources