I have read some topics explaining one of doing this, which would work, but would be incredibly slow. The explanation is here:
https://www.extendoffice.com/documents/excel/651-excel-remove-non-numeric-characters.html
It involves iterating through each cell in a range and then iterating through the characters in the field and removing them if they do not match [0-9]
. I wanted to see if anyone had any other more efficient suggestions.
One that comes to mind is loading the cell contents into an array, iterating through it, and splitting each entry into its own array to iterate through.
No need for VBA or for looping. An excel formula can achieve what you want.
This is an array formula. You have to press Ctrl + Shift + Enter
Explanation:
Each term is multiplied by the inverse of
(1+rate)^n
, wheren
is thenth
term in the series.By using different values for rate, we can get different results. In this case, using
-0.9
gives us1 + rate = 1 + -0.9 = 0.1
.Disclaimer: I did not come up with this formula. I had seen this formula long time ago and simply fell in love with it. Since then it has been a part of my databank.
For the VBA side of things (note the loops), I decided to satisfy my own curiosity about the performance of a couple different methods. All of them pull the range into an array and work on it in place. The linked article will get killed in speed by any of these, simply due to the overhead in reading and writing single cell values.
For the first method, I optimized the code from the linked article "a bit":
For the second method I used
RegExp.Replace
:Finally, for the last method I used a
Byte
array:Then I used this code to benchmark them against 1000 cells, each containing a random mix of 25 letters and numbers:
The results weren't horribly surprising - the Regex method is by far the fastest (but none of them are what I'd call "fast"):
Note that I have no idea how this compares to @SiddharthRout's cool formula method in that I can't run it through my testing harness. The www.extendoffice.com code would also probably still be running, so I didn't test it.