Consulting

Results 1 to 3 of 3

Thread: Loop through folder only opens one file

  1. #1
    VBAX Regular
    Joined
    Mar 2016
    Posts
    12
    Location

    Loop through folder only opens one file

    Hi,

    i wrote this code to loop threw the whole folder, but it only opens one and the same exelsheet in the folder and it doesnt switches to the next one. what am I doing wring?
    Sub AddNew()

    Dim s As Variant
    Dim s5 As Long
    Dim lastcell As Long
    Dim MyFile As String
    Dim directory As String



    MsgBox "Please open a file to show the path of the regions"
    s = Application.GetOpenFilename("Excel Workbook (*.xls; *.xlsx; *.xlsm),*.xls; *.xlsx; *.xlsm")
    s = Left(s, InStrRev(s, "\"))
    MyFile = Dir(s & "*.xl??")
    Debug.Print s

    Workbooks.Add
    's = Application.GetSaveAsFilename("WK00 - UK Total", "Excel Files (*.xlsm), *.xlsm")
    'ActiveWorkbook.SaveAs Filename:=s

    Do While MyFile <> ""
    Workbooks.Open (MyFile)

    s5 = 4

    Do While Cells(s5, 2) <> ""
    s5 = s5 + 1
    Loop

    Range(Cells(3, 2), Cells(s5, 13)).Copy
    ActiveWorkbook.Close

    If Cells(1, 1) <> "" Then
    ActiveCell.End(xlDown).Select
    lastcell = ActiveCell.Row
    Cells(lastcell + 1, 1).Select
    End If

    ActiveSheet.Paste
    Loop


    End Sub
    Best Regards

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    https://msdn.microsoft.com/en-us/lib...ffice.15).aspx


    QUOTE]
    You must specify pathname the first time you call the Dir function, or an error occurs. If you also specify file attributes, pathname must be included.
    Dir returns the first file name that matches pathname. To get any additional file names that match pathname, call Dir again with no arguments. When no more file names match, Dir returns a zero-length string (""). Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs. You can change to a new pathname without retrieving all of the file names that match the current pathname. However, you can't call the Dir function recursively. Calling Dir with the vbDirectory attribute does not continually return subdirectories.
    [/QUOTE]

    Inside the loop, you need to call Dir("") to get the next one


    Here's an example

    https://support.microsoft.com/en-us/kb/139724
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Mar 2016
    Posts
    12
    Location
    wow thank you that worked well. I didnt knew it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •