+ Reply to Thread
Results 1 to 10 of 10

VBA Macro to search for unique ID from one file in multiple sheets of another file

  1. #1
    Registered User
    Join Date
    04-05-2012
    Location
    Queensland, Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    VBA Macro to search for unique ID from one file in multiple sheets of another file

    Hi guys,

    I'm very new to this and just getting into programming so looking for some assistance with getting this Project going. I was after a macro that would search for a unique id such as 'CBX495-1' in column A, Row1 of one workbook (Workbook1) containing only one worksheet, in another workbook (Workbook2), containing multiple worksheets, using Workbook2's file name and path. I would like the macro to list where the unique id can be found in Column B of workbook 2.
    I would also like the Macro to move on to Row 2, Column A of Workbook one and repeat this until all rows in Workbook1 have been searched.
    Is this possible in excel?

    Any assistance would be greatly appreciated.

    Many thanks,
    Tim.
    Attached Files Attached Files
    Last edited by Timmo246; 04-05-2012 at 04:21 PM. Reason: Upload sample files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Hi Tim,

    Yes this is possible. Do you have sample files that you can upload?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    04-05-2012
    Location
    Queensland, Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Hi Arlette,

    Thanks for getting back to me so quickly. I have uploaded two files. File 'G_W_P' would be workbook1 and File 'G&W - S&C' would be workbook2. As mentioned in my original post. I was hoping the macro could look for a unique id# that it gets from Row 2 of Column A of 'G_W_P' within each worksheet of 'G&W - S&C' and when it finds that unique id, list the location of the worksheet in Column B of 'G_W_P' next to the unique id. I was hoping it could repeat this for the entire list in Column A of 'G_W_P'.
    I'm hoping that the macro will only display results that exactly match the unique Id#, and not numbers that contain the unique id but are different. For example, if unique id# 'CBX246' is searched, results should not include unique id#'s such as 'CBX246-1' or 'CBX246-2'.

    Thanks again for your help.
    Last edited by Timmo246; 04-05-2012 at 05:47 PM.

  4. #4
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Is the below what you wanted?
    Please Login or Register  to view this content.
    Last edited by PingPing; 04-07-2012 at 08:14 AM.

  5. #5
    Registered User
    Join Date
    04-05-2012
    Location
    Queensland, Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Hi Ping Ping,

    Thank you so much for your help. That code is fantastic and is far and away better than what I had written.

    Is it possible for the macro to search for multiple listings of the same unique ID , incase of double ups in sheets and list them in Column B, C, D etc. of "G_W_P.xlsm"
    Also, could the macro create a new sheet in "G_W_P.xlsm" and display the unique ID's that were searched for in "G&W - S&C.xlsx" but not found?

    Thanks so much for taking the time to help me with this one.

    Tim.

  6. #6
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Try this:
    Please Login or Register  to view this content.
    Last edited by PingPing; 04-10-2012 at 06:11 AM.

  7. #7
    Registered User
    Join Date
    04-05-2012
    Location
    Queensland, Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    That's awesome thanks Ping Ping. It works really well as I'm sure you know.
    Can you tell me why it doesn't work in different files that are set up the same way and in the same directory?

    I run the macro in a different spreadsheet that is set up the same way as wb1 & wb2, checking nearly the same unique ID's and it either runs and never stops or it gives me an application error.

    I change the workbook path for WB1 and the spreadshet name WB2 in the code but no luck.

  8. #8
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    I've updated my code to include some error handling (a Try/Catch/Finally methodology I use a lot).
    If there's an error it'll pause at the 'Stop: Resume' line on 'Stop'.
    You can then just step into the code (using F8). You'll see it move to 'Resume' then F8 again and it'll take you to the offending line of code.
    Let me know which one it is.
    If it, instead, 'runs and never stop' then there's a problem with the condition on my Do...While loop which will require further investigation (maybe you could post some offending files and give me an idea of where I should locate them on my machine, ie. directories for them).
    Last edited by PingPing; 04-10-2012 at 06:15 AM.

  9. #9
    Registered User
    Join Date
    04-05-2012
    Location
    Queensland, Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Hi Ping Ping,

    Thanks again for your help. You're very generous with your time and knowledge.

    I've included my code below, which is using a different filenames and worksheet names.

    The code is halting on the last 'Stop' command. I hit F8 repeatedly but the it never steps away from that line of code. Hope this helps, if not I'll post some offending files tomorrow.

    Many thanks again,
    Tim.

    Public Sub FindID2()
    On Error GoTo Catch

    Try:
    Dim dct As Object: Set dct = CreateObject("Scripting.Dictionary")

    Dim wkb1 As Workbook
    Dim wkb2 As Workbook
    Dim wks1 As Worksheet
    Dim wks2 As Worksheet
    Dim lngRowLast As Long
    Dim lng As Long
    Dim lngCount As Long
    Dim rng As Range
    Dim strToFind As String
    Dim strCellRef As String
    Dim strFirstAddress As String
    Dim var As Variant


    Set wkb1 = ThisWorkbook
    Set wkb2 = Workbooks.Open(wkb1.Path & "\Golden and Wathen - Scout_completion_status(18 3 12).xlsx") 'assumes wkb1 & wkb2 are in the same directory
    Set wks1 = wkb1.Worksheets("20120405-Gathering_Flowline_Pro")
    lngRowLast = wks1.Cells(wks1.Rows.Count, 1).End(xlUp).Row

    ' loop through each row in wks1
    For lng = 2 To lngRowLast

    dct.RemoveAll
    strToFind = wks1.Cells(lng, 1).Value

    ' loop through each worksheet in wkb2
    For Each wks2 In wkb2.Worksheets

    Set rng = wks2.Cells.Find(What:=strToFind, LookAt:=xlWhole) 'finds first (only) match in a worksheet
    If (Not rng Is Nothing) Then
    strFirstAddress = rng.Address
    Do

    strCellRef = "[" & wkb2.Name & "].[" & wks2.Name & "]." & rng.Address(False, False)
    If Not (dct.Exists(strCellRef)) Then Call dct.Add(strCellRef, "")
    Set rng = wks2.Cells.FindNext(rng)

    Loop While (Not rng Is Nothing And rng.Address <> strFirstAddress)
    End If

    Next wks2

    ' output the search results to wks1
    lngCount = 1
    For Each var In dct.Keys
    wks1.Cells(lng, 1).Offset(0, lngCount).Value = CStr(var)
    lngCount = lngCount + 1
    Next var
    Next lng

    ' widen columns to enable easy reading
    wks1.Columns.AutoFit

    ' add worksheet to dump 'Not Found' IDs
    Set wks2 = wkb1.Worksheets.Add

    Set rng = wks1.UsedRange
    With rng

    ' apply filter
    Call .AutoFilter(2, "=")

    ' copy rows to new worksheet
    Call .SpecialCells(xlCellTypeVisible).Copy(wks2.Range("A1"))

    End With

    ' turn off filter
    wks1.AutoFilterMode = False

    Finally:
    Exit Sub

    Catch:
    Stop: Resume

    End Sub

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA Macro to search for unique ID from one file in multiple sheets of another file

    Timmo246,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

+ 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