Very new to RegEx,
I have in cells in a column like:
I want![]()
Stuff, Other (please specify)
I have tried patterns like:![]()
Stuff
or![]()
"(, Other\(please specify\))"
Not getting it![]()
"(,\sOther\(please specify\))"
Thanks
Very new to RegEx,
I have in cells in a column like:
I want![]()
Stuff, Other (please specify)
I have tried patterns like:![]()
Stuff
or![]()
"(, Other\(please specify\))"
Not getting it![]()
"(,\sOther\(please specify\))"
Thanks
What is the relation to Excel?
I am using it in VBA
Thanks. VBA is not my forte. Didn't know that. Learned something today![]()
I am using like this
![]()
Sub RemoveRegEx_n() Dim x Dim R As Object Dim ws As Worksheet Dim rng1 As Range Dim lngRow As Long Set R = CreateObject("vbscript.regexp") Set ws = ThisWorkbook.Sheets("XXX") With R .Global = True .Pattern = (,\sOther\(please specify\)) .ignorecase = True End With Application.ScreenUpdating = False Set rng1 = ws.Range("A1:A" & ws.Cells(Rows.Count, 1).End(xlUp).Row) x = rng1.Value2 For lngRow = 1 To UBound(x, 1) x(lngRow, 1) = R.Replace(x(lngRow, 1), vbNullString) Next lngRow rng1.Value2 = x Application.ScreenUpdating = True End Sub
If you just the word stuff
Or![]()
"[Stuff]"
![]()
"[Stuff|sOther|(please specify)]"
Last edited by AB33; 02-14-2015 at 04:38 PM.
Apologies about my sloppy question, I have cells like:
or![]()
Civic Engagement, Community Building, Education, Leadership Development, Other (please specify)
There are more possibilities but all end with![]()
Arts, Culture, and Humanities, Community Building, Government and Public Administration, Spirituality/Religion, Other (please specify)
![]()
, Other (please specify)
What do you want to return or extract?
From the above
![]()
Civic Engagement, Community Building, Education, Leadership Development
I want to remove the![]()
Arts, Culture, and Humanities, Community Building, Government and Public Administration, Spirituality/Religion
from the ends of all the strings in my range![]()
, Other (please specify)
Thanks
Hi..
Rather than using Regex and most probably a loop.. try this..
![]()
Private Sub CommandButton21_Click() With Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) .Value = Application.Transpose(Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf)) End With End Sub
Nevermind - It didn't work![]()
Last edited by xladept; 02-14-2015 at 06:58 PM.
If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)
You can't do one thing. XLAdept
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin
lol.. mm.. yeap.. I may have gone a bit overboard on that one..
No - you didn't go overboard - my condensation worked when there was just one line - but, you're convolutions were necessary!
Why RegExp?
Just replace ", Other (please specify)" with "" should do....
If you insist
![]()
Sub test() Dim r As Range With CreateObject("VBScript.RegExp") .Pattern = "(.+)(?=(, Other \(please specify\)))" For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp)) If .test(r.Value) Then r.Value = .Execute(r.Value)(0) Next End With End Sub
Jindon's code is much better than mine and works in all circumstances, but I just post it.
Last edited by AB33; 02-14-2015 at 07:17 PM.
Ah, I just remembered your thread.
http://www.excelforum.com/excel-prog...g-nothing.html
For the learning purpose...
You want to remove
Stuff, Other (please specify)
So the patter should be
"(, Other \(please specify\))"
You missed a space after "Other"...
Thank you all for your input I greatly appreciate the different approaches, though I admit to being a bit lost on apo's solution
I was also trying to use the code below, but it was not replaceing the (please specify) in ", Other(please specify)", I could replace ", Other" then "(please specify)" but not ", Other(please specify)" in one go
![]()
Sub ReplaceUsingArray() Dim myBadChars As Variant Dim ws As Worksheet Dim Rng As Range Dim iCtr As Long Set ws = ThisWorkbook.Sheets("XXX") Set Rng = ws.Range("D2:D" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row) myBadChars = Array(", Other(please specify)") For iCtr = LBound(myBadChars) To UBound(myBadChars) Rng.Cells.Replace what:=myBadChars(iCtr), Replacement:="", _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False Next iCtr End Sub
Last edited by capson; 02-15-2015 at 12:08 PM.
Hi capson,
The beauty of APO's routine is that it does all the replacements at once - so it should be quickest.
Jindon's routine is vintage Jindon - elegantly cryptic - but it processes each phrase, not the whole thing all at once.
Jindon initially pointed out that you only needed a simple replacement - this routine processes each phrase so it can't be as quick as APO's routine:
![]()
Sub DumpPhrase(): Dim R As Range: For Each R In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) R.Value = Replace(R.Value, ", Other (please specify)", ""): Next: End Sub
Hi capson..
I will try to break it down for you so you might understand what i was thinking..though I admit to being a bit lost on apo's solution
Note to anyone else: If any of the following (technically or terminology) is incorrect... please let me know..
First.. try stepping through this code.. its' basically the same as the 1 line of code but I have separated it so each line does a specific thing..
IMPORTANT.. make sure your "Locals" window is open.
In the VB Editor.. go to "View" and enable the "Locals" window.
You will see something like this..![]()
Private Sub CommandButton1_Click() Dim a, b, c, d, e, f With Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) a = .Value b = Application.Transpose(.Value) c = Join(Application.Transpose(.Value), vbCrLf) d = Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", "") e = Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf) f = Application.Transpose(Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf)) .Value = Application.Transpose(Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf)) End With End Sub
Locals.png
Explanation:
a = Shows what the values would look lke when put into a 2 Dimensional array.
b = Transpose the values from the array into a 1 Dimensional array. This is so we can use the Join command next.
c = Join all the elements in the 1 Dimensional array (by the vbcrlf delimiter) into 1 long string.
d = Replace all instances of ", Other (please specify)" with "" in the long string.
e = Use the Split command (using the vbcrlf again as the delimter) to separate the lines into tseparate elements of a 1 Dimensional array.
f = Transpose it back to a 2 Dimensional array so it can written to the sheet correctly.
I hope that helps..
Wow, that helped a lot, I think I actually understood it!!!
You are all misunderstading what I offered at the first place, REPLACE
![]()
sub test columns(1).replace ", Other (please specify)","",2 end sub
Or even
![]()
sub test2() [a1:a1000] = [if(a1:a1000<>"",substitute(a1:a1000,", Other (please specify)",""),"")] end sub
Last edited by jindon; 02-15-2015 at 07:06 PM. Reason: fixed a Type
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks