+ Reply to Thread
Results 1 to 3 of 3

Macro to loop in folder and then open the workbook with found value

  1. #1
    Registered User
    Join Date
    05-02-2012
    Location
    NYC, NY
    MS-Off Ver
    Excel 2010
    Posts
    2

    Question Macro to loop in folder and then open the workbook with found value

    Hello,

    I am a total n00b in VBA

    Conditions:

    1. There are multiple workbooks in the folder S:\Schedules
    2. A fixed cell (G20) in active Workbook shows the Order Number that is currently running.

    Task:

    Create a macro that loops through S:\Schedules folder, searching for G20 value in the workbooks (without opening them), and then opens the workbook with matching Order (and if possible, highlights the row with that found Order number)

    Thanks for any help.

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Macro to loop in folder and then open the workbook with found value

    One cannot search a workbook without opening it. Otherwise see if the following helps.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    05-02-2012
    Location
    NYC, NY
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Macro to loop in folder and then open the workbook with found value

    Thanks Steve!

    I've changed some lines to make it work but your name is credited on the final version

    Sub Line_schedule()
    'Originated by Steve* on May 2, 2012
    Dim sSearchText As String, sPath As String, sFile As String
    Dim wb As Workbook, ws As Worksheet, rg As Range

    Application.ScreenUpdating = False

    'Change Path, need "\"
    sPath = "S:\Schedules" & "\"
    'Modify "*.xls*" if needed
    sFile = Dir(sPath & "*.xls*")

    sSearchText = Range("G20")
    If Len(sSearchText) = 0 Then Exit Sub
    Do While Len(sFile)
    If sFile <> ThisWorkbook.Name Then
    Set wb = Workbooks.Open(Filename:=sPath & sFile)
    If Not wb Is Nothing Then
    For Each ws In wb.Worksheets
    Set rg = ws.Cells.Find(sSearchText)
    If Not rg Is Nothing Then
    ws.Activate
    rg.Select
    Exit Do
    End If
    Next ws
    End If
    wb.Close SaveChanges:=False
    End If
    sFile = Dir()
    Loop
    Application.ScreenUpdating = True
    If wb Is Nothing Then
    MsgBox "The string: " & sSearchText & " was not found."
    End If
    End Sub

+ 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