VBA macro to find results

saurabhlotankar

New Member
Joined
Apr 20, 2016
Messages
21
Hello Everyone,

I have pasted a table below. From that table i want to extract codes(mentioned in column A) for each country with "Yes" along with country name and date mentioned at the top of each country.

this is not possible with formulas. Can anyone please help with me a vba code or macro to get this done. It will be really hekpful.

Thanks a lot.

11/28/201511/2/20154/10/20159/4/20154/27/20156/2/20154/30/2015
CodeNameUSUKGermanyTurkeyThailandIndiaSri Lanka
36915qYesNoNoYesYesNoNo
29417wYesYesYesNoNoNoYes
22928eNoYesNoNoNoNoYes
13201rYesYesNoNoNoYesNo
50053tYesNoNoYesYesYesYes
78369yYesYesNoYesYesNoNo
59904uNoYesYesYesYesNoYes
49654iNoYesYesYesNoNoNo
51644oYesNoNoYesYesNoNo
54172pNoYesYesNoYesYesYes
76530lYesYesNoNoNoNoNo
79272kNoYesYesYesNoYesYes
48911jNoYesYesNoNoNoYes
70934hNoNoNoYesYesYesNo
8767gNoYesYesNoNoNoNo
8961fYesYesYesNoYesNoNo
26324dYesNoYesYesYesYesNo
61679sYesYesNoYesNoYesYes
29962aYesYesNoNoYesNoYes
72390zNoYesNoYesNoNoNo
22474xYesNoYesYesNoYesNo
27881cNoYesYesNoYesNoNo
10897vYesYesYesNoYesNoYes
63988bNoNoYesYesYesYesNo
68620nNoNoYesYesNoYesYes
36840mNoNoYesYesYesYesNo

<colgroup><col><col><col><col span="2"><col><col><col><col></colgroup><tbody>
</tbody>




Result should look like this:
CodeNameCountrydate
36915qUS11/28/2015
29417wUS11/28/2015
13201rUS11/28/2015
50053tUS11/28/2015
78369yUS11/28/2015
51644oUS11/28/2015
76530lUS11/28/2015
8961fUS11/28/2015
26324dUS11/28/2015
61679sUS11/28/2015
29962aUS11/28/2015
22474xUS11/28/2015
10897vUS11/28/2015
29417wUK11/2/2015
22928eUK11/2/2015
13201rUK11/2/2015
78369yUK11/2/2015
59904uUK11/2/2015
49654iUK11/2/2015
54172pUK11/2/2015
76530lUK11/2/2015
79272kUK11/2/2015
48911jUK11/2/2015
8767gUK11/2/2015
8961fUK11/2/2015
61679sUK11/2/2015
29962aUK11/2/2015
72390zUK11/2/2015
27881cUK11/2/2015
10897vUK11/2/2015

<colgroup><col span="3"><col></colgroup><tbody>
</tbody>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Data would look like this


Excel 2010
ABCDEFGHI
111/28/201511/02/201504/10/201509/04/20154/27/201506/02/20154/30/2015
2CodeNameUSUKGermanyTurkeyThailandIndiaSri Lanka
336915qYesNoNoYesYesNoNo
429417wYesYesYesNoNoNoYes
522928eNoYesNoNoNoNoYes
613201rYesYesNoNoNoYesNo
750053tYesNoNoYesYesYesYes
878369yYesYesNoYesYesNoNo
959904uNoYesYesYesYesNoYes
1049654iNoYesYesYesNoNoNo
1151644oYesNoNoYesYesNoNo
1254172pNoYesYesNoYesYesYes
1376530lYesYesNoNoNoNoNo
1479272kNoYesYesYesNoYesYes
1548911jNoYesYesNoNoNoYes
1670934hNoNoNoYesYesYesNo
178767gNoYesYesNoNoNoNo
188961fYesYesYesNoYesNoNo
1926324dYesNoYesYesYesYesNo
2061679sYesYesNoYesNoYesYes
2129962aYesYesNoNoYesNoYes
2272390zNoYesNoYesNoNoNo
2322474xYesNoYesYesNoYesNo
2427881cNoYesYesNoYesNoNo
2510897vYesYesYesNoYesNoYes
2663988bNoNoYesYesYesYesNo
2768620nNoNoYesYesNoYesYes
2836840mNoNoYesYesYesYesNo
Sheet1


The code is below
Code:
Sub MacroToSortOut()
    Dim I As Long, K As Long, LastRow As Long, P As Long, ColStrt As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    ColStrt = 12
    
    P = 3
    For J = 3 To Cells(1, Columns.Count).End(xlToLeft).Column
        For I = 3 To LastRow
            If Cells(I, J).Value = "Yes" Then
                Cells(P, ColStrt) = Cells(I, 1)
                Cells(P, ColStrt + 1) = Cells(I, 2)
                Cells(P, ColStrt + 2) = Cells(2, J)
                Cells(P, ColStrt + 3) = Cells(1, J)
                P = P + 1
            End If
        Next I
    Next J
End Sub

The Colstrt determines the first column the result is pasted
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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