+ Reply to Thread
Results 1 to 18 of 18

Search for a column across multiple sheets and copy them in 1st sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Search for a column across multiple sheets and copy them in 1st sheet.

    Hi,
    I have a work book with sheet 1 containing ID numbers in column A. Remaining sheets have 3 columns ID no, name and Age. Each sheet corresponds to a different department and is named based on the department. I have attached an example file. What i want is to search for each ID in the column A of 1st sheet
    in all other sheets and copy the corresponding Age. it would be neat to have the department name on top of the corresponding column in sheet 1. please refer to the example sheet/
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    If I'm understanding you correctly, you can do this with a formula. Use the formula below in C2, fill right and fill down:

    =IFERROR(VLOOKUP($A2,INDIRECT(C$1&"!$A$2:$C$5",TRUE),3,FALSE),0)
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    Taking a second look, you should expand the range for your bigger sheets. Something like:

    =IFERROR(VLOOKUP($A2,INDIRECT(C$1&"!$A$2:$C$30",TRUE),3,FALSE),0)
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    thanks for your reply...so I used this and everything is marked 0 in the sheet..my data is in different sheets..i havent used INDIRECT before so dont know how it works to pin point the exact issue with formula

  5. #5
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    INDIRECT allows you to use a variable to apply different sheet names. If the header in your columns exactly matches the sheet name, the formula should perform the lookup on the sheet specified by the header. If you look at my attachment in post 3, the header in row 1 tells INDIRECT which sheet to look at. If you insert a space in C1, turning it into "Department 1" instead of "Department1", then the lookup fails and you get zeroes. Double-check your sheets to be sure that the headers on your lookup sheet exactly match the sheet names.

  6. #6
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    ah..i understand..thanks for your explanation..but i dont have the column headers already..in sheet 1 all i have is column A. that is it

  7. #7
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    So... it'll work then? Just make sure that when you develop the rest of your actual sheet1, the names of your department sheets are in sheet1 cells C1, D1, E1, etc.

  8. #8
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    it should..but there are like 250 sheets..and their names are random..i just put department1 and 2 for example...thats why i wanted a macro to do this

  9. #9
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    so i copied sheet names but the formula still doesnt work

  10. #10
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    Am getting #REF! on the cell if i donot suppress the error, somehow it is not picking up the reference..

  11. #11
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    It's hard for me to diagnose further without seeing a sample that shows what you're seeing, since it works on your initial sample. If you'd prefer a VBA solution anyway, though, try the following:

    Sub SheetLookup()
    Dim LR As Long
    Dim LC As Long
    Dim i As Long
    Dim j As Long
    Dim Master As String
    Dim LkSht As String
    
    
    Master = "Sheet1"  'Replace Sheet1 with name of master sheet
    
    Application.ScreenUpdating = False
    LR = Sheets(Master).Cells(Rows.Count, "A").End(xlUp).Row
    LC = Sheets(Master).Cells(1, Columns.Count).End(xlToLeft).Column
    For j = 3 To LC
        LkSht = Sheets(Master).Cells(1, j).Value
        For i = 2 To LR
            Sheets(Master).Cells(i, j).Formula = "=IFERROR(VLOOKUP(" & Range("A" & i) & ",'" & LkSht & "'!A2:C" & LR & ",3,FALSE),0)"
        Next i
    Next j
    Application.ScreenUpdating = True
    End Sub
    Keep in mind, though, that the headers in row 1 will still need to exactly match a sheet name.
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    Maybe this version will work better? The variation below will take the names of the sheets and turn them into your column headers, so you don't need any headers in row 1. The procedure will create the headers based on the sheet names, guaranteeing that they'll match:

    
    Sub SheetLookup()
    Dim LR As Long
    Dim LC As Long
    Dim i As Long
    Dim j As Long
    Dim Master As String
    Dim LkSht As String
    
    
    Master = "Sheet1"  'Replace Sheet1 with name of master sheet
    
    Application.ScreenUpdating = False
    LR = Sheets(Master).Cells(Rows.Count, "A").End(xlUp).Row
    LC = Sheets.Count
    For j = 2 To LC
        Sheets(Master).Cells(1, j + 1).Value = Sheets(j).Name
        LkSht = Sheets(Master).Cells(1, j + 1).Value
        For i = 2 To LR
            Sheets(Master).Cells(i, j + 1).Formula = "=IFERROR(VLOOKUP(" & Range("A" & i) & ",'" & LkSht & "'!A2:C" & LR & ",3,FALSE),0)"
        Next i
    Next j
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    thanks so much for your patience and help!! so i took out the ISerror part, and ran this code, i got #NAME? error on 1st 5 lines and then application defined or object defined error vba
    Sheets(Master).Cells(i, j + 1).Formula = "=IFERROR(VLOOKUP(" & Range("A" & i) & ",'" & LkSht & "'!A2:C" & LR & ",3,FALSE),0)"

  14. #14
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    How about this

    Sub abc()
     Const shMain As String = "Sheet1"
     Dim ws As Worksheet
     Dim i As Long
     Dim iMatch
     
     Application.ScreenUpdating = False
     For Each ws In Worksheets
        If ws.Name <> shMain Then
            With Worksheets(shMain)
                .Cells(1, 1).Offset(, ws.Index) = ws.Name
                For i = 2 To .Cells(Rows.Count, "a").End(xlUp).Row
                    iMatch = Evaluate("=VLOOKUP(" & .Cells(i, 1).Value & ",'" & ws.Name & "'!A:C,3,FALSE)")
                    If IsNumeric(iMatch) Then
                        .Cells(i, 1).Offset(, ws.Index) = iMatch
                    Else
                        .Cells(i, 1).Offset(, ws.Index) = 0
                    End If
                Next
            End With
        End If
     Next
     Application.ScreenUpdating = True
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  15. #15
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    Try this then.

    Sub abc()
     Const shMain As String = "Sheet1"
     Dim ws As Worksheet
     Dim i As Long
     Dim iMatch
     
     Application.ScreenUpdating = False
     For Each ws In Worksheets
        If ws.Name <> shMain Then
            With Worksheets(shMain)
                .Cells(1, 1).Offset(, ws.Index) = ws.Name
                For i = 2 To .Cells(Rows.Count, "a").End(xlUp).Row
                    iMatch = Application.Evaluate("=VLOOKUP(" & CStr(.Cells(i, 1).Value) & ",'" & ws.Name & "'!A:C,3,FALSE)")
                    If IsNumeric(iMatch) Then ' or try If Not Iserror(iMatch) then
                        .Cells(i, 1).Offset(, ws.Index) = iMatch
                    Else
                        .Cells(i, 1).Offset(, ws.Index) = 0
                    End If
                Next
            End With
        End If
     Next
     Application.ScreenUpdating = True
    End Sub
    Last edited by mike7952; 03-20-2017 at 03:42 PM.

  16. #16
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    ok..i think i figured out the issue, the values in column A if it is numeric the code is working, if its not it is not working.

    see the image below for Capture.PNG

    Cstr doesnt help

  17. #17
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    Does this work?

    iMatch = Application.Evaluate("=INDEX('" & ws.Name & "'!C:C,MATCH(A" & i & ",'" & ws.Name & "'!A:A,0))")

  18. #18
    Registered User
    Join Date
    12-17-2016
    Location
    south carolina, USA
    MS-Off Ver
    2013
    Posts
    84

    Re: Search for a column across multiple sheets and copy them in 1st sheet.

    this is great! working perfect..u sir are awesome!!

    Thanks so much for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Search multiple sheets and copy results to new sheet
    By augr in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-18-2014, 06:43 PM
  2. Replies: 3
    Last Post: 03-31-2014, 01:00 PM
  3. Replies: 10
    Last Post: 02-20-2014, 04:09 PM
  4. [SOLVED] Copy Rows to sheet if column value >40, multiple sheets
    By rlowe in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-04-2013, 06:08 PM
  5. [SOLVED] I need to search for a word on multiple sheets and copy the entire row to a new sheet
    By jkm750 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-26-2013, 12:13 AM
  6. [SOLVED] Search for Value in multiple columns and copy the value in column A to new sheet
    By hallr7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-02-2013, 11:20 AM
  7. Search for Multiple String Values on Multiple Sheets and Copy Rows to New Sheet
    By rrtikker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-15-2012, 12:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1