if (xx in cell) - then insert (column title)

Mignon

New Member
Joined
Feb 10, 2018
Messages
7
Hello guys

I need help to
1) search for "xx" in the table
2) if "xx" is in the cell then insert column title.

Something like this:

bea098a8-683a-4561-a421-d53b549eabdb



To something like this:
0833d81f-0ea3-4738-bc23-3d51aa9da258


TIA
Mignon

PS: I am new to Excel, working on Mac and cannot find out of programming yet, unless you can help me with step-by-step guide :)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Re: Help needed: if (xx in cell) - then insert (column title)

From this:

[TABLE="width: 1305"]
<!--StartFragment--> <colgroup><col width="87" span="6" style="width:65pt"> <col width="87" style="width:65pt"> <col width="87" span="8" style="width:65pt"> </colgroup><tbody>[TR]
[TD="class: xl89, width: 87"]date[/TD]
[TD="class: xl65, width: 87"]w22[/TD]
[TD="class: xl66, width: 87"]w12[/TD]
[TD="class: xl67, width: 87"]CAG[/TD]
[TD="class: xl68, width: 87"]PM[/TD]
[TD="class: xl66, width: 87"]CT[/TD]
[TD="class: xl66, width: 87"]MR[/TD]
[TD="class: xl90, width: 87"]EKKO[/TD]
[TD="class: xl91, width: 87"]SE/AE[/TD]
[TD="class: xl69, width: 174, colspan: 2"]Administration[/TD]
[TD="class: xl92, width: 87"]FRIKØBT[/TD]
[TD="class: xl93, width: 87"]Journalclub[/TD]
[TD="class: xl63, width: 174, colspan: 2"]Kongress[/TD]
[/TR]
[TR]
[TD="class: xl94, align: right"]1[/TD]
[TD="class: xl71, bgcolor: #92D050"] OK* AS**[/TD]
[TD="class: xl72, bgcolor: #92D050"]CT SG[/TD]
[TD="class: xl73, bgcolor: #92D050"]evt[/TD]
[TD="class: xl72, bgcolor: #92D050"]MK/SK[/TD]
[TD="class: xl72, bgcolor: #92D050"]IH[/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl72, bgcolor: #92D050"]TH[/TD]
[TD="class: xl75, bgcolor: #92D050"]KZ*[/TD]
[TD="class: xl76, bgcolor: #92D050, colspan: 2"] JD MH[/TD]
[TD="class: xl78"] [/TD]
[TD="class: xl74, bgcolor: #92D050"]EH[/TD]
[TD="class: xl79, bgcolor: #92D050, colspan: 2"]JJ HR MS JP[/TD]
[/TR]
[TR]
[TD="class: xl96, align: right"]2[/TD]
[TD="class: xl71, bgcolor: #92D050"]OK MH CV*[/TD]
[TD="class: xl72, bgcolor: #92D050"] MS IH AP[/TD]
[TD="class: xl73, bgcolor: #92D050"]evt[/TD]
[TD="class: xl72, bgcolor: #92D050"]SH[/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl80, bgcolor: #92D050"]JA[/TD]
[TD="class: xl75, bgcolor: #92D050"]TS[/TD]
[TD="class: xl81, bgcolor: #92D050, colspan: 2"] JD AS[/TD]
[TD="class: xl83, bgcolor: #92D050"]SK HD[/TD]
[TD="class: xl84"] [/TD]
[TD="class: xl71, bgcolor: #92D050, colspan: 2"]JJ HR AH EH[/TD]
[/TR]
[TR]
[TD="class: xl97, align: right"]3[/TD]
[TD="class: xl85, bgcolor: #92D050"]IH*'[/TD]
[TD="class: xl74, bgcolor: #92D050"]EH[/TD]
[TD="class: xl86"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl74"] [/TD]
[TD="class: xl87"] [/TD]
[TD="class: xl88"] [/TD]
[TD="class: xl85, bgcolor: #92D050, colspan: 2"]JJ[/TD]
[/TR]
[TR]
[TD="class: xl98, align: right"]4[/TD]
[TD="class: xl99, bgcolor: #92D050"]JA**[/TD]
[TD="class: xl100, bgcolor: #92D050"]IH[/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl100, bgcolor: #92D050"]EH[/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl100"] [/TD]
[TD="class: xl101"] [/TD]
[TD="class: xl102"] [/TD]
[TD="class: xl103, colspan: 2"] [/TD]
[/TR]
<!--EndFragment--></tbody>[/TABLE]


To this:
[TABLE="width: 174"]
<!--StartFragment--> <colgroup><col width="87" span="2" style="width:65pt"> </colgroup><tbody>[TR]
[TD="class: xl67, width: 87"]date[/TD]
[TD="class: xl65, width: 87, bgcolor: #92D050"]EH[/TD]
[/TR]
[TR]
[TD="class: xl68"]1[/TD]
[TD="class: xl63, bgcolor: #92D050"]Journalclub[/TD]
[/TR]
[TR]
[TD="class: xl69"]2[/TD]
[TD="class: xl64, bgcolor: #92D050"]Kongress[/TD]
[/TR]
[TR]
[TD="class: xl70"]3[/TD]
[TD="class: xl64, bgcolor: #92D050"]w12[/TD]
[/TR]
[TR]
[TD="class: xl71"]4[/TD]
[TD="class: xl66, bgcolor: #92D050"]MR[/TD]
[/TR]
<!--EndFragment--></tbody>[/TABLE]
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

This macro assumes that the first table is in Sheet2 and the second table is in Sheet1. Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your Sheet1 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a search value in cell B1 and exit the cell.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub
    Dim sAddr As String
    Dim foundVal As Range
    Set foundVal = Sheets("Sheet2").UsedRange.Find(Target, LookIn:=xlValues, lookat:=xlPart)
    If Not foundVal Is Nothing Then
        sAddr = foundVal.Address
        Do
            Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = Sheets("Sheet2").Cells(1, foundVal.Column)
            Set foundVal = Sheets("Sheet2").UsedRange.FindNext(foundVal)
        Loop While foundVal.Address <> sAddr
        sAddr = ""
    End If
End Sub
This macro works on a PC. I'm not sure if it will work on a Mac.
 
Last edited:
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

Hi @mumps

Thank you for the fast reply!

I get following error message in a new window:

(approx. translation from danish) Error on 'runningtime': '9':
The index is out of area
Button options: Cancel Troubleshooting

When I press Troubleshooting, the code window opens and line 9
(with text: 'Set foundVal = Sheets("Sheet2").UsedRange.Find(Target, LookIn:=xlValues, lookat:=xlPart)')


is tagged with an arrow in the left side.

Do you have any idea how to solve it?

TIA
Mignon
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

Hi again mumps!

I found the solution! Since my Excel is in danish, sheet2 was named in danish.

Thank you!
Mignon
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

You are very welcome. :)
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

You are very welcome. :)

An additional question:

how can I incorporate empty cells, so they are shown as empty in the result sheet? Right now are all the results with textstring in B1 listed without these empty spaces.

I have tried to fill the empty cells with '0', but it doesn't solve the problem so far.
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

I'm not sure what you mean. Can you explain in detail using a few examples from your data and referring to specific cells, rows, columns and sheets?
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

If I take the example from above, if I want to search for 'HD' in cell B2 in sheet1, the correct result will be:

[TABLE="width: 174"]
<!--StartFragment--> <colgroup><col width="87" span="2" style="width:65pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 87"]date[/TD]
[TD="class: xl64, width: 87"]HD[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl64"] [/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl64"]FRIKØBT[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]3[/TD]
[TD="class: xl64"] [/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]4[/TD]
[TD="class: xl64"] [/TD]
[/TR]
<!--EndFragment--></tbody>[/TABLE]


since there is only one match from the sheet2. The empty cells should stay empty
 
Upvote 0
Re: Help needed: if (xx in cell) - then insert (column title)

Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub
    Dim sAddr As String
    Dim foundVal As Range
    Set foundVal = Sheets("Sheet2").UsedRange.Find(Target, LookIn:=xlValues, lookat:=xlPart)
    If Not foundVal Is Nothing Then
        sAddr = foundVal.Address
        Do
            Cells(Sheets("Sheet2").Cells(foundVal.Row, 1).Value + 1, 2) = Sheets("Sheet2").Cells(1, foundVal.Column)
            Set foundVal = Sheets("Sheet2").UsedRange.FindNext(foundVal)
        Loop While foundVal.Address <> sAddr
        sAddr = ""
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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