[VBA] find punctuation in a string

Christian_Ku

Board Regular
Joined
Sep 10, 2007
Messages
102
Hello, I want to search for punctuation in strings. Is this possible or should i do this with a for loop or something like that?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello

What do you want to do with the punctuation? Simply test if there is any (TRUE / FALSE). Remove it?
 
Upvote 0
Well, actually I want to find the way ActiveCell.DirectPrecedent.Address is seperated, because sometimes this is with a , and sometimes with a :, and I don't know if other punctuations exist for the separation of precedent cells.
 
Upvote 0
It's easy:
Code:
MsgBox ActiveCell.DirectPrecedents.Address Like "*[,:]*"

Regards
Northwolves
 
Upvote 0
Ok, I can work that in an IF expression, now, does anyone know if there are other ways excel uses to split those cells. Or are the comma and semicolon the only ones?

Thakns for the replies!
 
Upvote 0
Hello

You can use this to test which punctuations are included:

Function GetPunc(str As String)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\w"
If .test(str) Then _
GetPunc = .Replace((str), "")
End With
End Function


applied as follows:

Sub ShowPunc()
MsgBox GetPunc(ActiveCell.DirectPrecedents.Address(0, 0))
End Sub



I think only , and : are possible. If your precedent references another sheet (e.g. Sheet1!A1) then that refence won't be included hence exclamation mark shouldn't feature.

What do you mean 'split those cells'? Do you mean you want to return each cell noted in the address? E.g. A1:A10 - we can split to show A1 and A10, or are you wanting to show each cell within that range?

If all you want is to return each cell referenced in the string then try this:

Function GetElement(ByVal Text As String, ByVal n As Long, Optional ByVal Delimiter As String = " ")
GetElement = Split(Text & String(n - 1, Delimiter), Delimiter)(n - 1)
End Function


Function PuncCount(str As String)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\W"
If .test(str) Then _
PuncCount = Len(str) - Len(.Replace((str), ""))
End With
End Function

Sub SplitString()
Dim MyString As String
MyString = ActiveCell.DirectPrecedents.Address(0, 0)
For i = 1 To PuncCount(MyString) + 1
MsgBox GetElement(Replace(MyString, ":", ","), i, ",")
Next i
End Sub


This assumes that the only punctuation / delimiters are comma's and colons.
 
Last edited:
Upvote 0
Thanks a bunch, it will take some time before I understand entirely what you have done here though ;)! Especially the functions are very useful (not only for the things I currently am working at). To answer your question: I wanted A1:A10 to split in an array of only A1 and A10, so that was a correct assumption.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top