VBA code for sorting text

engine22

Board Regular
Joined
Jul 5, 2007
Messages
62
Hi everyone,

Here is my problem:

I have a column of over 1000 cells with context in it. Each cell as a keyword in parathesis associated with the context. Therefore, different context are associated with the same keyword. However, the cells with the same keywords are spread out all over the 1000 cells.

Example:

cell1: anti-lock brake system (abs)
cell2: baromic pressure (Baro)
cell3: anti brake systematic (abs)
...


Basic solution:
I need to create a vba code that would look through the 1000 cells for a specific keyword and copy all the cells having that keyword in parathesis into another spreadsheet or column, so I can look through them easily. so if i look for (abs) it will give me:

cell1: anti-lock brake system (abs)
cell3: anti brake systematic (abs)
...

Ultimate solution:
The VBA code would be "smart" enough to copy all 1000 cells in keywords order. It would give me all possibilities:

cell1: anti-lock brake system (abs)
cell3: anti brake systematic (abs)
cell2: baromic pressure(Baro)
...

I hope i was clear enough and someone can help me... it would save me a ton of time...


Cheers!

-Engine22
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi engine22,

Do you need to keep the format you have.

If the Key Word was leading instead of trailing, you could just sort.

If that's not possible there are other solutions.

ColinKJ
 
Upvote 0
Hi,

Thank you for your help...

The thing is that sometimes the keyword (ABS) has a context that doesn't directly corresponds. Here are possible context for the keyword (ABS):

front wheel speed sensor (ABS)
4WAL 4 Sensor (ABS)
Traction Control System Hydraulic 4WAL (ABS)
4WAL 3 Sensor (ABS)

Therefore, I don't thing sorting would work or would it?

-Engine22
 
Upvote 0
In simple terms,

I basically want to sort the colums by the keyword found in the paranthesis... The paranthesis are mostly at the end of the cell, but it sometimes appear elsewhere...

Cheers,

engine22
 
Upvote 0
Hi engine22,

Are there ever more than one set of paranthesis in one cell,

I was thinking of moving the Key to the left and sorting like:

cell1: (abs) anti-lock brake system
cell3: (abs) anti brake systematic
cell2: (Baro) baromic pressure


ColinKJ
 
Upvote 0
Yeah that would work... but would I have to move the keyword manually to the front?

Because i have over a thousands cells so it would take me a long time to move those keywords to the front...

Let me know if you have a way of doing that...
 
Upvote 0
Greetings,

In a copy of your workbook (case I goober this up, we don't wreck your work), try:


In a Standard Module:
Code:
Sub redo()
Dim rcell As Range
For Each rcell In Selection
    If Not InStr(1, rcell, "(", vbTextCompare) = 0 _
    And Not InStr(1, rcell, ")", vbTextCompare) = 0 Then
 
 
        rcell.Value = _
        Trim(Right(rcell, Len(rcell) - (InStr(1, rcell, "(", vbTextCompare)) + 1) & _
        Chr(32) & _
        Left(rcell, Len(rcell) - (Len(rcell) - InStr(1, rcell, "(", vbTextCompare) + 1)))
    End If
Next
End Sub

Select the cells that you want changed around, press Alt + F8 and run. Remember to remove the macro/module lest you accidently run 'er again.

Hope this helps,


Mark
 
Upvote 0
Hi engine22,

If you struggle with Mark's solution, try pasting this in to a standard module
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Code:
Sub RearrangeAndSort()
nr = Sheets(1).Range("A65536").End(xlUp).Row
For a = 2 To nr
x = Cells(a, 1)
b = 1
st:
If Mid$(x, b, 1) <> "(" Then b = b + 1: GoTo st
c = 1
st1:
If Mid$(x, c, 1) <> ")" Then c = c + 1: GoTo st1
d = Mid$(x, b, (c - b) + 1) + Mid$(x, 1, b - 1)
Cells(a, 1) = d
Next a
Columns("A:A").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Cells(1, 1).Select
End Sub

Code:

This assumes the data is in Column A, and that you have a header in A1.

You will need to enable Macros first.

To run the code, goto Tools>Macro>Macro Select RearrangeAndSort then Click Run

Regards

ColinKJ
 
Upvote 0

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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