Help with existing code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I was given the code below to extract a word among text in a cell and it puts it in the adjacent cell. If I have quite a few words how do I enter them in the code. Also some may be a mixture of number and letters.

Another thing if the destination cell has data in it how do I put what is moved at the end of it rather than what it does now and overwrites what is in there?

Thanks.

Code:
Sub ExtractData()
Dim REX         As Object
Dim rexMatch    As Object
Dim rexMatchCol As Object 
Dim Cell        As Range
Dim strText     As String
     Set REX = CreateObject("VBScript.RegExp")
    With REX
        .Global = True
        .IgnoreCase = True 
        .Pattern = "Example"
 
        For Each Cell In Range(Cells(2, "J"), Cells(Rows.Count, "J").End(xlUp))
            If .test(Cell.Value) Then
                Set rexMatchCol = .Execute(Cell.Value)
                Cell.Value = .Replace(Cell.Value, vbNullString)
                strText = vbNullString
                For Each rexMatch In rexMatchCol
                    strText = strText & Chr(32) & rexMatch.Value
                Next
                Cell.Offset(, 1).Value = Trim(strText)
            End If
        Next
    End With
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Any chance of a small set of sample data and expected results?
 
Upvote 0
In the code at the moment it looks for the word 'Example' and moves that into the adjacent celland overwrites what is in that cell. In the example provided I would need the words Example, Morning and 90Bhp put in the code so they are removed from column A and put at the end in column B.

Then I could change the words in the code to suit once the code is made.

In the instance where there are a mixture of numbers and letters (90Bhp) it needs to look for either 2 or 3 numbers before the letters which I believe is written like this:- "\b[0-9]+Bhp\b". Thanks

Sheet1

<TABLE style="BACKGROUND-COLOR: #ffffff; PADDING-LEFT: 2pt; PADDING-RIGHT: 2pt; FONT-FAMILY: Calibri,Arial; FONT-SIZE: 11pt" border=1 cellSpacing=0 cellPadding=0><COLGROUP><COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 161px"><COL style="WIDTH: 133px"></COLGROUP><TBODY><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt; FONT-WEIGHT: bold"><TD> </TD><TD>A</TD><TD>B</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">1</TD><TD style="FONT-WEIGHT: bold">Before</TD><TD> </TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">2</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">This Is An Example</TD><TD>This Was A</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">3</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">Good Morning All</TD><TD>Good </TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">4</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">Engine 90Bhp Turbodiesel</TD><TD>Engine 80Bhp</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">5</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"> </TD><TD> </TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">6</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"> </TD><TD> </TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">7</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt; FONT-WEIGHT: bold">After</TD><TD> </TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">8</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">This Is An</TD><TD>This Was A Example</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">9</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">Good All</TD><TD>Good Morning</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">10</TD><TD style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">Engine Turbodiesel</TD><TD>Engine 80Bhp 90Bhp</TD></TR></TBODY></TABLE>
 
Upvote 0
Is it possible that more than one of the target words could appear in the same cell (eg "This morning I saw a good example")? If so, what should happen in the adjacent cell?
 
Upvote 0
Yes that is possible I suppose, they can be put together at the end of the data already in the adjacent cell. It doesn't matter where the words go in the adjacent cell really just that they dont overwrite what is already there.
 
Upvote 0
I may need to add say 15 words to the code, but these may appear 10000 times among 20000 possible rows at various places in that column/row.
 
Upvote 0
Try this modification to the existing code.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ExtractData()<br><SPAN style="color:#00007F">Dim</SPAN> REX         <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> rexMatch    <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> rexMatchCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Cell        <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> strText     <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> s           <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br>    <SPAN style="color:#00007F">Set</SPAN> REX = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">With</SPAN> REX<br>        .Global = <SPAN style="color:#00007F">True</SPAN><br>        .IgnoreCase = <SPAN style="color:#00007F">True</SPAN><br>        .Pattern = "Example|Morning|90Bhp" <SPAN style="color:#007F00">'<- Add more here if you want</SPAN><br> <br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Cell <SPAN style="color:#00007F">In</SPAN> Range(Cells(2, "J"), Cells(Rows.Count, "J").End(xlUp))<br>            s = Cell.Value<br>            <SPAN style="color:#00007F">If</SPAN> .test(s) <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> rexMatchCol = .Execute(s)<br>                Cell.Value = Trim(.Replace(s, vbNullString))<br>                strText = vbNullString<br>                <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rexMatch <SPAN style="color:#00007F">In</SPAN> rexMatchCol<br>                    strText = strText & Chr(32) & rexMatch.Value<br>                <SPAN style="color:#00007F">Next</SPAN><br>                Cell.Offset(, 1).Value = Cell.Offset(, 1).Value & strText<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Note that this may leave multiple consecutive spaces in your original column (I've used J as the original code used that column).

Note also that it may remove just part of a word. ("I have some good examples")

If either of these are problems, some more tweking of the code would be needed.
 
Upvote 0
Thanks, before I try it is it possible to add "\b[0-9]+Bhp\b" to it as there will be several possible Bhps e.g. 90, 95, 100, 110 etc etc.. and they will all need moving. Also the space thing isn't a problem as I can run a trim macro I have after.
 
Upvote 0
Thanks Peter that seems to work great. Is there any limit as to how many words I can add. Also how much would it complicate things if I wanted to move certain words to say 3 columns across?
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,521
Members
452,923
Latest member
JackiG

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