+ Reply to Thread
Results 1 to 6 of 6

VBA double-loop search and copy

Hybrid View

  1. #1
    Registered User
    Join Date
    03-12-2014
    Location
    Central Texas
    MS-Off Ver
    Excel 2010
    Posts
    55

    Unhappy VBA double-loop search and copy

    Hey ya'll,
    Once again, I've searched the forum and Google for the answer and I cannot find it, so I'm hoping it's locked away in someone's wisdom on this forum.
    I don't know how to begin with this one, but I've got an idea what I need.

    What I'm looking for is, as the title suggests, Is a loop within a loop to search a specific cell on every worksheet, in every file in a specific list. I've tried searching for "search" VBA codes, for "loop" VBA codes and I don't know either good enough to bend them to my needs, nor to combine them (usually, I just end up messing the whole thing up so I can't even fix it).

    In the posted example, I put the three pages for the example and manually entered the information I'm looking for.
    The basics are:
    1) Outer loop: search for a file, with an increment-able cell reference as part of the filename.
    2) Inner loop: search each worksheet, with an increment-able cell reference as the whole worksheet name.
    3) Copy the cell $D$1 (of the example) off each worksheet to the table on the MasterTrack file.
    Each worksheet in the Searched-For file is a specific name represented by the cells of Column A on the YahBoh worksheet of the MasterTrack file.
    Each Searched-For file begins with "Yadayada" and contains a specific name in the cells of Row1 on the YahBoh worksheet of the MasterTrack file.
    The same cell ($D$1 of the example) will be copied from every worksheet, so there's no need for a 'search term' with that.

    I hope I explained this clear enough. If you have a start, a website that'll show something relevant, anything will help.

    Please, thank you.
    Attached Files Attached Files
    Oh! I get it. I need rep--->҉

  2. #2
    Registered User
    Join Date
    08-01-2012
    Location
    Montreal, Qc, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: VBA double-loop search and copy

    Hi, I didnt open up your files, but i think i understand what your looking for.

    this should help point you in the right direction if nothing else. ( there might be a little bit of additional data that you don't need. i tried to remove it all but i pulled this from one of my old codes.)

    let me know if it works

    Sub Transfer_data_from_Files()
    
    Dim MyPath, MyFileName, MyFolderName
    Dim PATHandFILE, FExist, MyCount, FCount, MySheet
    
    
    '****************************************************************
    ' set path to folders
    
    MyPath = "C:\Users\*your user name*\Desktop\Myfolder"     ' be sure to change the username and the folder where your files are (do not put the main one in same folder)
    
    MyFileName = Dir(MyPath & "\")
    
    
    '****************************************************************
    'count files with in dir
    
    Do While MyFileName <> ""
           MyCount = MyCount + 1
            MyFileName = Dir()
    Loop
    
    FCount = MyCount
    MyCount = 0
    
    '****************************************************************
    'set first file name
    
    MyFileName = Dir(MyPath & "\")
    
    
    '****************************************************************
    ' loop throw each file copy the data
    
    Do While MyFileName <> ""
       PATHandFILE = MyPath & MyFileName
       
        Workbooks.Open Filename:=PATHandFILE
        Workbooks(MyFileName).Activate
        
       For MySheet = 1 To Workbooks(MyFileName).Sheets.Count
            Workbooks(MyFileName).Sheets(MySheet).Range("D1").Copy
            
            ThisWorkbook.Activate
            
            MyRow = Sheet1.Range("A99999").End(xlUp).Row + 1
            Sheet1.Cells(MyRow, 1).Select
            Sheet1.Cells(MyRow, 1).PasteSpecial
        Next
            
        Application.DisplayAlerts = False
        Workbooks(MyFileName).Close
        Application.DisplayAlerts = True
        
        
        MyCount = MyCount + 1
      
        
        
       MyFileName = Dir()
    
    Loop
    
    
    End Sub

  3. #3
    Registered User
    Join Date
    03-12-2014
    Location
    Central Texas
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: VBA double-loop search and copy

    Steve, technically your code did look in the directed folder, took the information I was looking for and copied it to the Master file. However: 1) the code itself was very confusing and I wasn't sure where to interject my example files' to get it to run on the example sheet and 2) when the data was copied to the Master file, it copied over the bottom row instead of in the table.

  4. #4
    Registered User
    Join Date
    09-05-2014
    Location
    Montreal
    MS-Off Ver
    2007
    Posts
    22

    Re: VBA double-loop search and copy

    Hi here is what you need:

    Option Explicit
    Sub doubleLoop()
    Dim c As Range, c2 As Range
    Dim strFileName As String, Path As String
    Dim strIncrFileName As String, strIncrWksName As String
    Dim wks As Worksheet, wksTmp As Worksheet
    Dim wkbTmp As Workbook
    
    For Each c In ThisWorkbook.Worksheets("YahBoh").Range("B1:D1") 'Loops through the files names increments
    
        strIncrFileName = "Yadayada" + CStr(c) + "*" '* will work for any format
        Path = "C:\Users\stagiaire\Desktop\Forums questions\" 'PLUG YOUR PATH HERE
        strFileName = Dir(Path & strIncrFileName) 'Gets the file name
        
        If strFileName <> vbNullString Then 'If a file has been found
        
            For Each c2 In ThisWorkbook.Worksheets("YahBoh").Range("A2:A4") 'Loops through the worksheets names increments
                
                Set wkbTmp = Workbooks.Open(Path & strFileName, , True) 'Opens and sets the temporary workbook read-only
                
                strIncrWksName = c2
                
                Set wksTmp = wkbTmp.Worksheets(strIncrWksName)
                
                ThisWorkbook.Worksheets("YahBoh").Cells(c2.Row, c.Column) = wksTmp.Range("$D$1")
                                
            Next
            
            wkbTmp.Close False 'Closes the temporary workbook without saving
            
        End If
        
    Next
    
    End Sub
    It works with the examples sheets you provided, you just have to replace the names, path and ranges accordingly to your needs.

  5. #5
    Registered User
    Join Date
    03-12-2014
    Location
    Central Texas
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: VBA double-loop search and copy

    ggs22,
    That works great! I don't know how you made it look so simple, but that's just what I was looking for! Thank you, so much.

  6. #6
    Registered User
    Join Date
    09-05-2014
    Location
    Montreal
    MS-Off Ver
    2007
    Posts
    22

    Re: VBA double-loop search and copy

    Its a pleasure! (don't forget to mark it as solved)
    Last edited by ggs22; 07-30-2015 at 01:24 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Double click cell - copy data to new sheet - create a list from double clicking
    By kakky in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-04-2015, 11:14 PM
  2. double loop until the end (?)
    By pccamara in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-21-2015, 10:45 AM
  3. Copy double-clicked cell to next empty row in different sheet with each double-click
    By erhathaway in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-25-2013, 12:06 AM
  4. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  5. loop Search, copy paste,next search, copy paste ...
    By Macrodroid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-11-2011, 03:19 AM
  6. a double loop?
    By jimmy1981 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-03-2010, 10:48 AM
  7. If else loop to search data and copy it between workbooks
    By exc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-04-2009, 02:51 PM

Tags for this Thread

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