VBA Loop Through All Files in a Folder

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024
  • Grab the Free VBA Quick Reference Guide
    chrisjterrell....
    In this Video we use VBA to Loop through all the Files in a specific Folder Path.
    To do this we use a For loop to loop through each file in the folder path and then place the file names on the worksheet
    code
    ===============
    Sub AllFiles()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    rw = 10
    fle = Range("C6") & "\"
    Set objFolder = objFSO.getfolder(fle)
    For Each objFile In objFolder.Files
    If Right(objFile.Name, 4) = "xlsx" Then
    Cells(rw, 3) = objFile.Name
    Cells(rw, 4) = objFile.datecreated
    Cells(rw, 5) = objFile.parentfolder.Path
    rw = rw + 1
    End If
    Next
    End Sub

Комментарии • 34

  • @hunterakers7096
    @hunterakers7096 4 года назад +1

    Thank you for helping me understand the logic behind this! Now my program is working properly!

  • @TheCamelBug
    @TheCamelBug 3 года назад +1

    Thanks for all the info sir! Can you show me how to go to a file and pull out specific cells from all workbooks from that file and bring them to another workbook?

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  3 года назад +1

      You can put 15 minutes on my calendar at my website.
      Go to chrisjterrell.com and sign up for the 15 minute coaching

  • @datadev1
    @datadev1 8 лет назад +1

    Nice, I have an immediate use for this code. Thanks for sharing.

  • @guillaumeremy4165
    @guillaumeremy4165 3 года назад +1

    Thank you very much! Nice and helping video

  • @antonioramosch
    @antonioramosch 2 года назад +1

    Hola, no se ingles, pero me funciona muy bien, busque mucho tiempo como hacerlo, Gracias..... - Thanks for sharing -

  • @dlee1224
    @dlee1224 2 года назад

    Maybe I came in on Chapter 3 but why are rw and fle used when not defined? Is there another version of this program for beginners?

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  2 года назад

      I typically don't declare my variables in the videos because it is another thing to explain.
      If a variable is not declared in VBA it is a Variant by default. Variants are pretty dynamic

  • @raymondjurado9203
    @raymondjurado9203 7 лет назад +1

    Good stuff. Thanks for sharing.

  • @MagnusAnand
    @MagnusAnand 8 лет назад +1

    Why do you prefer this method to using Dir() and interesting until you have no more files?

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  8 лет назад +1

      Completely agree this works great until there are a lot of files. This video works well for going through tons of files quickly.
      ruclips.net/video/ddA2_SOaq14/видео.html

  • @janezklun
    @janezklun 8 лет назад +1

    Nice, helpful video,
    Thank you for sharing

  • @kevinmcinturf8976
    @kevinmcinturf8976 3 года назад +1

    What would be the best way to list folders in folders?

  • @mohammadahmed7932
    @mohammadahmed7932 4 года назад +1

    Sir can you provide a VBA for changing header and footer in folder having 12 files(workbooks) with one worksheet.because i do not know to create a VBA. thanks.

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  3 года назад

      I would start by recording a video making the change. This should help you head the correct direction

    • @mohammadahmed7932
      @mohammadahmed7932 3 года назад +1

      @@EverydayVBAExcelTraining thanks

  • @mananaeem
    @mananaeem 4 года назад +2

    I need code to collect data from different workbooks books in folder and copy each relevant sheet in master file.
    I have three different file in folder and have 3 works sheets in each file. I need to loop through each file and copy relevant sheet to relevant master file. Than data from next file to master workbook. Waiting for reply, please Naeem

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  4 года назад +1

      Have you watched any of my videos on Knime? That might be the best solution

    • @mananaeem
      @mananaeem 4 года назад

      Please share link that will help me

    • @mananaeem
      @mananaeem 4 года назад

      Loop through different sheet and relevant sheet copy to master sheet... in short my query. Plz share

    • @mananaeem
      @mananaeem 4 года назад

      I have seen the video. Need help on my query

  • @jojocarrillo
    @jojocarrillo 4 года назад +1

    thank you Chris... but what I want to do is copy the files from specific folders but I only have the partial folder names in a list. Can you help me out?

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  4 года назад

      You will most likely use the instr function is will give you index if a string is found and zero if it is not.

    • @jojocarrillo
      @jojocarrillo 4 года назад

      @@EverydayVBAExcelTraining can you guide me through this? I'm really new at VBA.

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  4 года назад

      Cool could you shoot me an email at chris@chrisjterrell.com and I will see if I can help

    • @jojocarrillo
      @jojocarrillo 4 года назад +1

      @@EverydayVBAExcelTraining thanks Chris I just sent the email today and hoping for some assist. thanks

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  4 года назад

      Check your email I sent a link to fill out 😁

  • @andysun4405
    @andysun4405 5 лет назад +1

    fantastic video.after I look up all files, say 10 excel files in the folder, how can I copy some specific content from 10 files into one workbook

    • @EverydayVBAExcelTraining
      @EverydayVBAExcelTraining  5 лет назад

      That would take some work. Do you know how to open a workbook

    • @andysun4405
      @andysun4405 5 лет назад

      EverydayVBA hey yeah
      I was thinking to use code such as:
      Dim fso, excel
      Folder=“C\target”
      Set fso=creatsobject(“scripting.filesysetmobject”)
      Excel=creatobject(“excel.application”)
      For each file in fso.getfolder(folder).files
      Set W=excel.workbooks.open(filepath)
      W.save
      W.close
      End sub