PICK OUT/EXTRACT column header "COMMON" IN ALL 5 SHEETS

panyagak

Active Member
Joined
Feb 24, 2017
Messages
299
Hi Excel gurus

Please help.

My 4 worksheets are named: Wk1, Wk2, Wk3 &
Wk4, Wk5. In the 5 sheets, columns A (Date) and B ( Subject name) are similar. I need a vba code to
pick out only"Subject names" that ARE COMMON in ALL THE 5 sheets and paste them in a new sheet, "New All".

Am tired of copy, paste & sort more than 20,000 rows!!

HELP!!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Can the names repeat in the same sheet?

If not it's just a simple case of looping through each name in Wk1 and if it is found in all other sheets place it in New All

If it does then need to check that the name has been searched before.
 
Upvote 0
A good question Gallen.

Yes the names might repeat 2, 3, 4 times but with differebt values in one or more sheets..

Picking just one is ok or as you deem fit

regards
Patrick
 
Upvote 0
This will do it if names don't repeat or doesn't matter if they do

Code:
Sub FindAll()

    Dim rNames As Range, Name As Range, rFound As Range
    Dim lr As Long 'Last row in Wk1
    Dim nr As Long ' next available row in New All
    Dim i As Integer 'counter for worksheets
    
    
    lr = Worksheets("Wk1").Range("A" & Rows.Count).End(xlUp).Row
    nr = Worksheets("New All").Range("A" & Rows.Count).End(xlUp).Row + 1
    
    'Get Names range
    Set rNames = Worksheets("Wk1").Range("B2:B" & lr)
    
    For Each Name In rNames
        For i = 2 To 5 'loop though worksheets 2 to 5
            'find an exact match
            Set rFound = Worksheets("Wk" & i).Range("B:B").Find(What:=Name, LookAt:=xlWhole)
            If rFound Is Nothing Then 'name not found so don't enter on New All
                GoTo NextName
            End If
        Next i
        'only get here if name is found in each sheet
        Name.EntireRow.Copy
        Worksheets("New All").Range("A" & nr).PasteSpecial xlPasteAll
        nr = nr + 1
        Application.CutCopyMode = False
NextName:
    Next Name
    
End Sub
 
Last edited:
Upvote 0
Solution
Gallen.

Yes names do repeat themselves in 2 to 3 sheets each with different values. each sheet is filled with 5 different colours - hope they"re maintained in the final sheet.

Patrick
 
Upvote 0
Ok so if they repeat and you don't want to search for them again:

Code:
Sub FindAll()
    Dim rNames As Range, Name As Range, rFound As Range
    Dim lr As Long 'Last row in Wk1
    Dim nr As Long ' next available row in New All
    Dim i As Integer 'counter for worksheets
    Dim sNamesSearched As String 'String to hold values of all names searched for
    
    
    lr = Worksheets("Wk1").Range("A" & Rows.Count).End(xlUp).Row
    nr = Worksheets("New All").Range("A" & Rows.Count).End(xlUp).Row + 1
    
    'Get Names range
    Set rNames = Worksheets("Wk1").Range("B2:B" & lr)
    
    For Each Name In rNames
        If InStr(1, sNamesSearched, Name) > 0 Then GoTo NextName 'if name already searched skip the name
        sNamesSearched = IIf(sNamesSearched = "", Name, sNamesSearched & "," & Name) 'record the name searched for
        For i = 2 To 5 'loop though worksheets 2 to 5
            'find an exact match
            Set rFound = Worksheets("Wk" & i).Range("B:B").Find(What:=Name, LookAt:=xlWhole)
            
            If rFound Is Nothing Then 'name not found so don't enter on New All
                GoTo NextName
            End If
        Next i
        'only get here if name is found in each sheet
        Name.EntireRow.Copy
        Worksheets("New All").Range("A" & nr).PasteSpecial xlPasteAll
        nr = nr + 1
        Application.CutCopyMode = False
NextName:
    Next Name
    
End Sub
 
Upvote 0
Gallen.

will try both out in the course of the day & get back in case of any hitch.

thank you & God bless for quick resolution.
 
Upvote 0
Gallen & thanks for your robust reply.

Have gone through your code & spotted these in quotes
"If"
"sNamesSearched = IIf(sNamesSearched = "", Name, sNamesSearched & "," & Name) 'record the name
searched for
For i = 2 To 5 'loop though worksheets 2 to 5"
"through"

Should the code remain the way it is, OR its 2 errors - "If" & "through" - to correct.

regards
Patrick
 
Upvote 0
Gallen & thanks for your robust reply.

Have gone through your code & spotted these in quotes
"If"
"sNamesSearched = IIf(sNamesSearched = "", Name, sNamesSearched & "," & Name) 'record the name
searched for
For i = 2 To 5 'loop though worksheets 2 to 5"
"through"

Should the code remain the way it is, OR its 2 errors - "If" & "through" - to correct.

regards
Patrick

Yes 'though' should read 'through', simple typo in a comment.

No the IIF is correct it does the "If Then End If" all in one line. if you google IIF Vba you will get a clear explanation
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,847
Members
452,361
Latest member
d3ad3y3

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