+ Reply to Thread
Results 1 to 6 of 6

VBA double-loop search and copy

Hybrid View

Old4xford VBA double-loop search and... 07-29-2015, 01:03 PM
Steve7913 Re: VBA double-loop search... 07-30-2015, 10:23 AM
Old4xford Re: VBA double-loop search... 07-30-2015, 12:32 PM
ggs22 Re: VBA double-loop search... 07-30-2015, 10:54 AM
Old4xford Re: VBA double-loop search... 07-30-2015, 12:33 PM
ggs22 Re: VBA double-loop search... 07-30-2015, 01:11 PM
  1. #1
    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.

  2. #2
    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.
    Oh! I get it. I need rep--->҉

  3. #3
    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