+ Reply to Thread
Results 1 to 9 of 9

Excel Macro search and copy function in subfolders

Hybrid View

  1. #1
    Registered User
    Join Date
    10-17-2018
    Location
    k
    MS-Off Ver
    k
    Posts
    5

    Excel Macro search and copy function in subfolders

    Hello everybody,
    i've again some problems with my VBA-Code and hopefully some of you can help me.
    The macro search in a directory after excel files with certain names and then copy the data to another workbook, this works perfect thanks to the hepf of this forum!
    But know, i want that the macro not only copy the data from this folder but als from the subfolders and i struggle to realize that.
    I'm really a newbie in VBA, so if anyone could help me with this, that would be great!

    Here's my code:
    Sub Suchenundkopieren()
    
    Dim directory As String, fileName As String, sheet As Worksheet
    Application.ScreenUpdating = False
    
    directory = "C:\Users\YL\Desktop\Test\*"
    fileName = Dir(directory & "Quell*.xl*")
    
    Tabelle1.Cells.Clear
    
    
        Do While fileName <> ""
    
        Dim src As Workbook
        Dim lr As Long 'Quelle
        Dim lrZiel As Long 'Ziel
        Dim WsZiel As Worksheet
    
            Set WsZiel = ThisWorkbook.Worksheets("Tabelle1")
    
            Set src = Workbooks.Open(directory & fileName, Password:="1234")
    
            lr = src.Worksheets("Overview").Range("A" & src.Worksheets("Overview").Rows.Count).End(xlUp).Row
    
            lrZiel = WsZiel.Cells(WsZiel.Rows.Count, 1).End(xlUp).Row
            If lrZiel <> 1 Then lrZiel = lrZiel + 2
    
            src.Worksheets("Overview").Range("A1:MV" & lr).Copy Destination:=WsZiel.Cells(lrZiel, 1)
    
        src.Close False
    
        Set src = Nothing
        Set WsZiel = Nothing
    
        fileName = Dir
    
        Loop
    
    Application.ScreenUpdating = True
    
    End Sub
    Greetings
    Last edited by alansidman; 11-06-2018 at 01:32 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2507 Win 11
    Posts
    24,919

    Re: Excel Macro search and copy function in subfolders

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Excel Macro search and copy function in subfolders

    Maybe like this
    Sub aa()
    Dim fso as object,sf as object,v as object
    Dim ff as object
    Set fso = CreateObject("scripting.fileSystemObject")
    Set sf = fso.getFolder("c:\mydoc\")
      For each ff in sf.subfolders
          Set ff = sf
       
             For each v in ff.files
                 Msgbox v.name
                  'do somethink
             Next v
      Next ff
    End sub
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  4. #4
    Registered User
    Join Date
    10-17-2018
    Location
    k
    MS-Off Ver
    k
    Posts
    5

    Re: Excel Macro search and copy function in subfolders

    I've tried to implement it but i just get som error notification. Could you tell me what is wrong?

    Here's my modified code:
    Sub Suchenundkopieren()
    
    Dim directory As String, fileName As String, sheet As Worksheet
    Dim fso As Object, sf As Object, v As Object, ff As Object
    
    Application.ScreenUpdating = False
    
    directory = "C:\Users\YL\Test\*"
    fileName = Dir(directory & "Quell*.xl*")
    
    Tabelle1.Cells.Clear
       Set sf = fso.getFolder("C:\Users\YL\Test\")
       For Each ff In sf.subfolders
       Set ff = sf
            Do While fileName <> ""
    
            Dim src As Workbook
            Dim lr As Long 'Quelle
            Dim lrZiel As Long 'Ziel
            Dim WsZiel As Worksheet
    
                Set WsZiel = ThisWorkbook.Worksheets("Tabelle1")
    
                Set src = Workbooks.Open(directory & fileName, Password:="1a2b3c4d")
    
                lr = src.Worksheets("Overview").Range("A" & src.Worksheets("Overview").Rows.Count).End(xlUp).Row
    
                lrZiel = WsZiel.Cells(WsZiel.Rows.Count, 1).End(xlUp).Row
                If lrZiel <> 1 Then lrZiel = lrZiel + 2
    
                src.Worksheets("Overview").Range("A1:MV" & lr).Copy Destination:=WsZiel.Cells(lrZiel, 1)
    
            src.Close False
    
            Set src = Nothing
            Set WsZiel = Nothing
    
            fileName = Dir
    
            Loop
        Next ff
        
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by gedingen_35; 11-07-2018 at 06:52 AM.

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Excel Macro search and copy function in subfolders

    TRY THIS
    Sub wBopEN()
    Dim fso As Object, f As Object, sf As Object, ssf As Object, myFile As Object
    Dim WB As Workbook, j As Long, n As Long, i As Long, x(), mytable
    Set fso = CreateObject("Scripting.filesystemObject")
    Set f = fso.GetFolder("C:\Users\YL\Test\")  'THIS IS MAIN FOLDER
    
    Application.ScreenUpdating = False
    For Each sf In f.SubFolders
    Set ssf = sf
        For Each myFile In ssf.Files
           If myFile.Name Like "Quell*.xl*" Then
              MsgBox myFile.Name
              Set WB = Workbooks.Open(myFile, False)
                With WB.Sheets("Overview")
                       lr = .Worksheets("Overview").Range("A" & .Worksheets("Overview").Rows.Count).End(xlUp).Row
                       .Worksheets("Overview").Range("A1:MV" & lr).Copy ThisWorkbook.Worksheets("Tabelle1").Cells(Rows.Count, 1).End(3)(2)
                 .Parent.Close savechanges:=False
                End With
           End If
        Next myFile, sf
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Registered User
    Join Date
    10-17-2018
    Location
    k
    MS-Off Ver
    k
    Posts
    5

    Re: Excel Macro search and copy function in subfolders

    I've tried it but become an error post(438) in this line:

    lr = .Worksheets("Overview").Range("A" & .Worksheets("Overview").Rows.Count).End(xlUp).Row
    Do you have an idea what could be the problem?

  7. #7
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513
    First yo must select main folder
    Folder a contain sub folder 1 and folder 2
    Your path must select
    "c:\blallalal\a\"
    Its work for me
    Last edited by jeffreybrown; 11-07-2018 at 07:14 PM. Reason: Removed full quotes!

  8. #8
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513
    lr = .Worksheets("Overview").Range("A" & .Worksheets("Overview").Rows.Count).End(xlUp).Row
    Change to
    .Worksheets("Overview").columns(1).specialcells(11).row
    Last edited by jeffreybrown; 11-07-2018 at 07:14 PM. Reason: Removed full quotes!

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Excel Macro search and copy function in subfolders

    daboho,

    Please don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.
    HTH
    Regards, Jeff

+ 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. macro search in folder and subfolders
    By max_max in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 06-13-2018, 04:15 PM
  2. Replies: 4
    Last Post: 10-21-2016, 05:33 PM
  3. Replies: 1
    Last Post: 10-19-2016, 04:44 PM
  4. Needs macro to copy excel files from folders, subfolders to new folder
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-08-2014, 08:53 AM
  5. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  6. [SOLVED] Search subfolders in excel 2010
    By greasybob in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-08-2012, 07:03 PM
  7. Replies: 2
    Last Post: 03-26-2012, 07:12 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