+ Reply to Thread
Results 1 to 17 of 17

Code for searching multiple worksheets and copying matching data to 1 w/sheet

  1. #1
    Registered User
    Join Date
    02-12-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi there,

    I have a workbook with various worksheets. On specific worksheets there is a table where I add issues with equipment, with the date issue arose and a rectified date when resolved. I now have a search worksheet where i want to find any un-rectified issues across all the tables on the different worksheets. The code i have written so far finds un-rectified issues on one worksheet and brings it to the search sheet. My problem is how to make it loop through the other worksheets and do the same check.

    Here is my code so far;

    Sub search_button1_click()

    Dim r As Integer

    'Finds last row of data

    r = 30000

    Do Until Sheet11.Cells(r, 4).Value <> ""
    r = r - 1

    Loop

    r = r + 1

    ' Puts data onto sheet

    Sheet48.Cells(r, 1) = Sheet11.Cells(r, 1).Value
    Sheet48.Cells(r, 2) = Sheet11.Cells(r, 2).Value
    Sheet48.Cells(r, 3) = Sheet11.Cells(r, 3).Value
    Sheet48.Cells(r, 4) = Sheet11.Cells(r, 4).Value






    End Sub
    Last edited by lambi15; 03-08-2013 at 09:45 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Lambi,
    Please wrap your code with code tags.
    If you could attach a sample, I will try to write you a different code. Your current code requires lots of adjustments, we have to write it as if from the start.
    To attach a sample excel book. Go to advance, half way the page you see an attachment.

  3. #3
    Registered User
    Join Date
    02-12-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Smile Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi AB33,

    I have attached the whole workbook for you to see for yourself. You will see that there are various worksheets that have the issue table on and others which list the equipment. The search worksheet is the one where if I was to click the CMDbutton it would complete the search of all the tables.

    Thank you for your help

    Lambi
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Lambi,

    "The code i have written so far finds un-rectified issues"
    I need to know what you are searching for. Are you searching for a word, or value? I know you wanted to search across sheets, in all cells, but I need to know what to search. In your code, you have indicated columns D of search sheet, but column D is empty.

  5. #5
    Registered User
    Join Date
    02-12-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi AB33,

    The search wants to find any issues that have been logged but haven't yet been rectified(no date in column 4), the search checks each table and then if the row has data and column 4 is empty it shows that as a result on the search page.

  6. #6
    Registered User
    Join Date
    02-12-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi AB33,

    The search wants to find any issues that have been logged but haven't yet been rectified(no date in column 4), the search checks each table and then if the row has data and column 4 is empty it shows that as a result on the search page.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    I think my understanding is you want to search for any data that has been logged-on. I have added few lines to make your code to loop through each sheet, but I do not know how to search for empty value. There was a similar thread in this formum which deals with loggin code and if you are interested, I can send you the link.

    Please Login or Register  to view this content.

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

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Maybe this

    Please Login or Register  to view this content.
    Last edited by mike7952; 03-08-2013 at 09:43 AM.
    Thanks,
    Mike

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

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi, AB33,

    your code loops through the sheets but will always copy from the sheet with Codename Sheet11, and there is not splitting between the search sheet and the sheet on which to loop through.

    @lambi15:
    IŽd recommend to add a column at Column A showing the name of the worksheet where the issue is to be found (which is missing in this piece of code):
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi Holger,
    It was not my code, but rather OP's one. I only added 2-3 lines to OP's lines.

  11. #11
    Registered User
    Join Date
    02-12-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Hi Everyone,

    Thank you for your help, it is now working thanks to Holger's code.

  12. #12
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Quote Originally Posted by AB33 View Post
    I think my understanding is you want to search for any data that has been logged-on. I have added few lines to make your code to loop through each sheet, but I do not know how to search for empty value. There was a similar thread in this formum which deals with loggin code and if you are interested, I can send you the link.

    Please Login or Register  to view this content.
    Would = vbNullString be better than <> "" ?

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    GazzaDn,
    Yes, you are right!
    "" could include a cell which appears to be empty to our eyes because a formula could return nil but in excel, it is not empty. So, to be absolutley sure you should use the isempty function to test if a cell is indeed empty.

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

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    See below. Both vbNullString and ="" work the same.

    Please Login or Register  to view this content.

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    Mike,
    "" is not the same as empty. I have read in a VBA written by three guys(Still have got that book) whose advice was "" is not always empty, it could include a cell which returns what looks empty but it is not.

  16. #16
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    I could see a scenario where a task takes multiple days and someone uses a formula to return "" as the answer until complete.

    The above would find the Value "" and write it to the summary as un-rectified and a duplicate work order could be raised. A little far fetched maybe, but these things can happen when the usual person doing the task is on Holiday.

    Perhaps its a subtle difference between un-rectified and un-addressed.

  17. #17
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Re: Code for searching multiple worksheets and copying matching data to 1 w/sheet

    double post

+ 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