macro to pull all data from every file in a folder and copy Data into the open workbook, then delete the file before the loop

lauhelp

New Member
Joined
May 25, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a code that opens each excel file in a folder, copies a range from that file and paste it on the other main file which I call consolidated database. This part of the code works fine, what is troubling me is that it only deletes all the file in that folder when the loop has been completed. Here's part of my current code that deletes all of them:

Sub
My Code here to open, copy, then paste...

Loop

Kill "C:\Users\[Redacted - My User ID]\OneDrive - [Redacted - My Company's Name]\Temp\*.xls"
End Sub

What I want to happen is to add a code to delete each file after it has been opened, information has been copied, and then pasted to database, and closed... just before opening another file. Reason is that when loop process fails sometime, it will not delete those files that have been recorded to my main database already, and I would have to manually check thousand of files, when I am processing a batch file for consolidation.

I hope my narration makes sense.

Many thanks in advance!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You need to show your code which loops through the folder. If you're using a Dir function loop you can delete the current file with a Kill statement before getting the next file, like this:
VBA Code:
    Dim fileName As String
    fileName = Dir("C:\folder\path\*.xls")
    While fileName <> vbNullString
        
        'Code to open and close fileName
        
        Kill "C:\folder\path\" & fileName
        fileName = Dir
    Wend
 
Upvote 0
You need to show your code which loops through the folder. If you're using a Dir function loop you can delete the current file with a Kill statement before getting the next file, like this:
VBA Code:
    Dim fileName As String
    fileName = Dir("C:\folder\path\*.xls")
    While fileName <> vbNullString
      
        'Code to open and close fileName
      
        Kill "C:\folder\path\" & fileName
        fileName = Dir
    Wend
Hi @John_w,

Appreciate you looking into this, here's my actual code:

VBA Code:
Sub UpdateDatabase()

'Consolidate
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Dim we As Worksheet
    Set wkbDest = ThisWorkbook
    
    Const strPath As String = "C:\Users\UserID\OneDrive\Temp\"
    ChDir strPath
    strExtension = Dir("*.xls")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open (strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("Sheet1").Range("A2:BV" & LastRow).Copy wkbDest.Sheets("Database").Cells(Rows.Count, "A").End(xlUp).Offset(1,0)
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = Truew

'Delete all files in the folder

Kill "C:\Users\UserID\OneDrive\Temp\*.xls"

End Sub

I want to be ale to delete the file after the code .Close savechanges:=False.

Thanks John!
 
Upvote 0
As I said, a Kill statement before getting the next file. Therefore replace:
VBA Code:
        strExtension = Dir
with:
VBA Code:
        Kill strPath & strExtension
        strExtension = Dir
 
Upvote 0
Solution
As I said, a Kill statement before getting the next file. Therefore replace:
VBA Code:
        strExtension = Dir
with:
VBA Code:
        Kill strPath & strExtension
        strExtension = Dir
Works like a charm! Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,801
Messages
6,121,644
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top