Consulting

Results 1 to 5 of 5

Thread: Search all worksheets within a spreadsheet for a value and copy data to another sheet

  1. #1
    VBAX Regular
    Joined
    Jun 2010
    Posts
    90
    Location

    Search all worksheets within a spreadsheet for a value and copy data to another sheet

    Hey all,

    I have a workbook with over a 100 worksheets, can some code be written to autofilter by a value, and then search all the worksheets for this value and copying the data from each of the sheets where the value is found to another woroksheet?

    Thanks

    Phil

  2. #2
    VBAX Regular ntrauger's Avatar
    Joined
    May 2011
    Location
    Monticello
    Posts
    38
    Location
    Your description is pretty vague, but I bet this will help get you on the right track.[vba]Sub SearchAllSheets()
    Dim ws As Worksheet, dest As Worksheet
    Dim c As Range
    Dim firstAddress As String
    Dim i As Long
    Dim val
    val = "Data to Search For" 'String, date, or whatever.
    Set dest = Sheet3 'Destination sheet.
    i = 1
    For Each ws In Worksheets
    If ws.Name = dest.Name Then GoTo skip
    Set c = ws.Cells.Find(val)
    If Not c Is Nothing Then
    firstAddress = c.Address
    Do
    c.EntireRow.Copy dest.Cells(i, 1)
    i = i + 1
    Set c = ws.Cells.FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstAddress
    Set c = Nothing
    End If
    skip:
    Next
    End Sub[/vba]
    Edit: Something to be aware of is that this code may give unexpected results if your target data occurs in more than one column in the same row on the same sheet.

  3. #3
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Even if the data is in many columns and you are concerned with a specific column say 1 then you can edit above code as:
    [VBA] Do
    If c.Column = 1 Then
    c.EntireRow.Copy dest.Cells(i, 1)
    i = i + 1
    End if
    Set c = ws.Cells.FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstAddress
    [/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  4. #4
    VBAX Regular
    Joined
    Jun 2010
    Posts
    90
    Location
    Hey guys,

    Thanks for your replies, with the modification this works a treat sorry about the vague explanation will be clearer when I post next time.

    Thanks again for your help saved me manually autofiltering and copying data from over a hundred worksheets!!! =

    Phil

  5. #5
    VBAX Regular ntrauger's Avatar
    Joined
    May 2011
    Location
    Monticello
    Posts
    38
    Location
    Glad to help. Don't forget to take a second and mark the thread as solved using the "Thread Tools" menu at the top right of the page.

Posting Permissions

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