I would like to parse a cell column of strings in Excel VBA, each of the cell contains the following:

2 '␂'

I would like to change the contents of this cell to contain only 2 (or any numeric characters just before the 'string'. What would be the easiest method of parsing such info? Thanks.

Was it helpful?


If you are sure about the format, find the string before the ' (apostrophe) & change the cell value. Assuming you are on the cell that has this value, use the following VBA code.

activecell.text = mid(activecell.text, 1, instr(1, activecell.Text, "'")-1)

It looks for an apostrophe and extracts characters before it & puts it into current cell.


Assuming that the numeric data is always at the start of the cell, you can use Regex to find any consecutive numbers at the start of the string, stopping once it hits the first non-numeric characters. Here is a function that will do it for you, I hope it helps. The ^ stands for start of the cell, and (\d+) means one or more numeric characters and the .* means followed by zero or more characters. I am assuming there is only one string of numbers you need, so I have the regex just return submatch 0 (first match).

Function ExtractNumericData(ByVal text As String) As String

Application.ScreenUpdating = False
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.Pattern = "^(\d+).*"
RE.Global = True
Set allMatches = RE.Execute(text)
ExtractNumericData = allMatches.Item(0).submatches.Item(0)
Application.ScreenUpdating = True

End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top