+ Reply to Thread
Results 1 to 7 of 7

Searching for data across multiple sheets and copying to one main sheet

Hybrid View

truedonk Searching for data across... 11-20-2008, 11:32 AM
Simon Lloyd If your code works for you... 11-20-2008, 11:55 AM
truedonk Had a play around, but still... 11-23-2008, 09:55 AM
StephenR An alternative approach: ... 11-23-2008, 11:28 AM
truedonk I'm getting the following... 11-23-2008, 12:13 PM
StephenR On which line does it error?... 11-24-2008, 07:58 AM
royUK Is the data in tables with... 11-24-2008, 08:13 AM
  1. #1
    Registered User
    Join Date
    11-20-2008
    Location
    Manchester UK
    Posts
    4

    Searching for data across multiple sheets and copying to one main sheet

    Hi there, i'm currently using this macro, which is helping me find data in one sheet and copying it to another. However I now need to search for the data across multiple worksheets and then copy it to one worksheet.

    Dim LSearchRow As Integer
    Dim LCopyToRow As Integer
    Dim LSearchValue As String
    
    On Error GoTo Err_Execute
    
    LSearchValue = InputBox("Please enter a value to search for.", "Enter value")
    
    Sheets("V.Sat").Select
    Range("A1").Select
    
    'Start search in row 4
    LSearchRow = 2
    
    'Start copying data to row 2 in Sheet2 (row counter variable)
    LCopyToRow = 2
    
    While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    
    'If value in column E = LSearchValue, copy entire row to Sheet2
    If Range("E" & CStr(LSearchRow)).Value = LSearchValue Then
    
    
    
    'Select row in Sheet1 to copy
    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
    Selection.Copy
    
    'Paste row into Sheet2 in next row
    Sheets("Get Info").Select
    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
    ActiveSheet.Paste
    
    'Move counter to next row
    LCopyToRow = LCopyToRow + 1
    
    'Go back to Sheet1 to continue searching
    Sheets("V.Sat").Select
    
    End If
    
    LSearchRow = LSearchRow + 1
    
    Wend
    
    'Position on cell A3
    Application.CutCopyMode = False
    Range("A3").Select
    
    MsgBox "All matching data has been copied."
    Basically, once it's finished searching on the v.sat worksheet, it then moves to the q.sat, neither, q.dis and v.dis worksheets.

    Any help would be really appreciated.

  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    If your code works for you then this adaptation should work:

     
    Dim LSearchRow As Integer
    Dim LCopyToRow As Integer
    Dim LSearchValue As String
    Dim Sh As Worksheet
    On Error GoTo Err_Execute
    LSearchValue = InputBox("Please enter a value to search for.", "Enter value")
    For Each Sh In Sheets
    If Sh.Name = "Get Info" Then
    Else
    'Start search in row 4
    LSearchRow = 2
    'Start copying data to row 2 in Sheet2 (row counter variable)
    LCopyToRow = 2
    While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    'If value in column E = LSearchValue, copy entire row to Sheet2
    If Range("E" & CStr(LSearchRow)).Value = LSearchValue Then
     
    'Select row in Sheet1 to copy
    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
    Selection.Copy
    'Paste row into Sheet2 in next row
    Sheets("Get Info").Select
    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
    ActiveSheet.Paste
    'Move counter to next row
    LCopyToRow = LCopyToRow + 1
    
    End If
    LSearchRow = LSearchRow + 1
    Wend
    End If
    Next Sh
    'Position on cell A3
    Application.CutCopyMode = False
    Range("A3").Select
    MsgBox "All matching data has been copied."
    Not all forums are the same - seek and you shall find

  3. #3
    Registered User
    Join Date
    11-20-2008
    Location
    Manchester UK
    Posts
    4
    Had a play around, but still no joy. The macro I have there is something that i lifted and shifted from another site.

    Maybe I should look at starting a new macro rather than changing that one. Basically i'm looking for a macro (like the one i've copied), which will allow me to search for a criteria. Excel will then look through the sheets called V.Sat, Q.Sat, Neither, Q.Dis, V.Dis and copy the row which contains that criteria into a sheet called 'Get Info'. It will copy the row beneath the last one and continute until it's found all of the row with that criteria in. Once it's found and copied all of those rows, it will stop.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    An alternative approach:
    Sub x()
    
    Dim r As Long, rngFound As Range, ws, i As Long, nFind As Long
    
    ws = Array("V.Sat", "Q.Sat", "Neither", "Q.Dis", "V.Dis")
    
    nFind = InputBox("Please enter a value to search for.", "Enter value")
    
    For i = LBound(ws) To UBound(ws)
        With Sheets(ws(i))
            Set rngFound = .Cells(1, 1)
            For r = 1 To WorksheetFunction.CountIf(.Cells, nFind)
                Set rngFound = .Cells.Find(What:=nFind, After:=rngFound, LookIn:=xlValues, Lookat:=xlWhole)
                If Not rngFound Is Nothing Then _
                    rngFound.EntireRow.Copy Sheets("Get Info").Cells(Rows.Count, 1).End(xlUp)(2)
            Next r
        End With
    Next i
    
    End Sub

  5. #5
    Registered User
    Join Date
    11-20-2008
    Location
    Manchester UK
    Posts
    4
    I'm getting the following error with this:

    Runtime error '13':

    Type mismatch

    Thanks so much for the help so far by the way!

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    On which line does it error? Do you know how to check the code using F8? If not, post a small sample workbook.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Is the data in tables with header rows?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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