I am very new to VB, but I need a small macro that performs the following:
I select a number of cells, and then when I run the macro, it should look for text in brackets and delete it, including the brackets:
the car (2) provided with a roof (8 and 9) comprising
- a clutch (34) with a hand brake (3)
etc
should result in
the car provided with a roof comprising
- a clutch with a hand brake
I more or less managed to substitute a concrete text (for example "clutch") but here the macro has to look for "(" with
FIND("(";B10) and afterwards find the next bracket ")"
and then perform
REPLACE form "(" to ")" and replace it by ""
Could someone give me a hand?
Thanks.
Try this
provided your text is in cell A1=SUBSTITUTE(SUBSTITUTE(A1,")",""),"(","")
regards
johnjohns
When you are not sure where to go, every road takes you there!
It "kind of works".
If in A1 we have:
word1 (123) word2 (456)
it will result in:
word1 123 word2 456
no brackets, but still the content of the brackets will be there.
the result I am looking for is:
word1 word2
Thanks a lot
You can try the find/replace option in excel, where find what is (*) and keep the replace with as empty. And if want to do it with vba, record a macro while replacing.
regards
johnjohns
When you are not sure where to go, every road takes you there!
Hi raparigo74
Maybe a UDF
Function Match_Replace(Rng As Range) Dim RegEx As Object Match_Replace = Rng.Text Set RegEx = CreateObject("vbscript.regexp") With RegEx .Global = True .Pattern = "(\([\w\s]+\))" Match_Replace = RegEx.Replace(Match_Replace, String("$1", Chr(32))) End With Set RegEx = Nothing End Function
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
OK, I am very new in VB, but why it does not begin with
Sub example()
and ends with End Sub
I do not manage to make it work
hi raparigo74,
UDF is user defined function.. you can make functions like the built-in excel worksheet functions.
http://www.techbookreport.com/tutorials/excel_vba1.html
so place the code in a module you can then use the "Match_Replace" UDF
eg
the result in B1 will be "word1 word2 "a1 = word1 (123) word2 (456) B1= Match_Replace(A1)
It is recommended to use "Function"s instead of "Sub"s in both worksheets and VBA
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Hello Pike,
You could reduce the Pattern to this:
.Pattern = "(\(.+\))"
This will catch all characters rather than space, 0-9,A-Z,a-z, and underscore. If there is a chance that the parentheses could be empty then use this pattern:
.Pattern = "(\(.*\))"
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Hi Leith,
thanks for the pointer
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks