lukasz_rz,
Thanks for the workbook.
The macro (using three arrays in memory) will adjust for a varying number of words in row 1, and, for a varying number of strings beginning in cell F5, down.
Sample raw data:
Excel 2007
| A | B | C | D | E | F | G |
---|
1 | cow | dog | giraffe | elephant | parrot | | |
2 | | | | | | | |
3 | | | | | | | |
4 | | | | | | | |
5 | | | | | | cow makes muu | |
6 | | | | | | giraffe is tall | |
7 | | | | | | elephant is big | |
8 | | | | | | parrot is a colorful bird | |
9 | | | | | | each little dog is cute | |
10 | | | | | | dog eats meat | |
11 | | | | | | giraffe eats leafes | |
12 | | | | | | elephant has a huge trump | |
13 | | | | | | parrot can make human's voices | |
14 | | | | | | giraffe can be met in Africa | |
15 | | | | | | Once I had a dog but it ran away. | |
16 | | | | | | some cows give a lot of milk | |
17 | | | | | | elephant is the biggest animal on the planet | |
18 | | | | | | elephant is bigget than a parrot | |
19 | | | | | | | |
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
After the macro:
Excel 2007
| A | B | C | D | E | F | G |
---|
1 | cow | dog | giraffe | elephant | parrot | | |
2 | | | | | | | |
3 | | | | | | | |
4 | | | | | | | |
5 | cow | | | | | cow makes muu | |
6 | giraffe | | | | | giraffe is tall | |
7 | elephant | | | | | elephant is big | |
8 | parrot | | | | | parrot is a colorful bird | |
9 | dog | | | | | each little dog is cute | |
10 | dog | | | | | dog eats meat | |
11 | giraffe | | | | | giraffe eats leafes | |
12 | elephant | | | | | elephant has a huge trump | |
13 | parrot | | | | | parrot can make human's voices | |
14 | giraffe | | | | | giraffe can be met in Africa | |
15 | dog | | | | | Once I had a dog but it ran away. | |
16 | cow | | | | | some cows give a lot of milk | |
17 | elephant | | | | | elephant is the biggest animal on the planet | |
18 | elephant / parrot | | | | | elephant is bigget than a parrot | |
19 | | | | | | | |
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).
1. Copy the below code
2. Open your NEW workbook
3. Press the keys
ALT +
F11 to open the Visual Basic Editor
4. Press the keys
ALT +
I to activate the Insert menu
5. Press
M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys
ALT +
Q to exit the Editor, and return to Excel
8. To run the macro from Excel press
ALT +
F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Code:
Option Explicit
Sub ExtractWords()
' hiker95, 10/13/2013
' http://www.mrexcel.com/forum/excel-questions/732529-extracting-words-word-string.html
Dim L, a As Variant, f As Variant
Dim i As Long, ii As Long, h As String, c As Long
L = Range(Cells(1, 1), Cells(1, Cells(1, Columns.Count).End(xlToLeft).Column))
f = Range("F5:F" & Range("F" & Rows.Count).End(xlUp).Row)
ReDim a(1 To UBound(f, 1), 1 To 1)
For i = LBound(f, 1) To UBound(f, 1)
h = ""
For c = LBound(L, 2) To UBound(L, 2)
If InStr(f(i, 1), Trim(L(1, c))) > 0 Then
h = h & Trim(L(1, c)) & " / "
End If
Next c
If Right(h, 3) = " / " Then
a(i, 1) = Left(h, Len(h) - 3)
End If
Next i
With Range("A5").Resize(UBound(f, 1))
.ClearContents
.Value = a
.Columns(1).AutoFit
End With
End Sub
Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension
.xlsm
Then run the
ExtractWords macro.