Clearing the data from mutiple workbooks using VBA

NixLouise

New Member
Joined
May 6, 2015
Messages
10
Hello, I am brand new to VBA and I have some code to transfer data from multiple spreadsheets into one master spread sheet. This works ok but at the end I need to clear the days data from each of the spreadsheets so the data can be transferred again the next day with out duplicating. Is there something simple I can add to the end of my script to clear all the data I have just transferred? I know this should be easy but I just can get over this last hurdle. Any help would be very greatly appreciated
Here is my script so far:

Sub LoopThroughDirectory()

Dim MyFile As String
Dim erow
Dim Filepath As String

Filepath = "C:\Users\Guy\Desktop\nichola\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "ZMASTER.xlsm" Then
Exit Sub
End If

Workbooks.Open (Filepath & MyFile)
Range("A2:E25").Copy
ActiveWorkbook.Close

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 5))

MyFile = Dir
Loop
End Sub
 
Hello, I tried this under the range I already have and it comes up with Run-time error '1004' paste method worksheet class failed
 
Upvote 0
Hello, I tried this under the range I already have and it comes up with Run-time error '1004' paste method worksheet class failed
You can't clear the copied range before you paste those contents. You can try this (untested):
Code:
Sub LoopThroughDirectory()
Dim myBk As Workbook
Dim MyFile As String
Dim erow
Dim Filepath As String
Set myBk = ThisWorkbook
Filepath = "C:\Users\Guy\Desktop\nichola\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
    If MyFile = "ZMASTER.xlsm" Then
    Exit Sub
    End If
    
    Workbooks.Open (Filepath & MyFile)
    With Range("A2:E25")
        .Copy
        With myBk.ActiveSheet
            erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            .Paste Destination:=.Cells(erow, 1)
        End With
        .ClearContents
    End With
    ActiveWorkbook.Close savechanges:= True
    MyFile = Dir
Loop
End Sub
 
Last edited:
Upvote 0
You can't clear the copied range before you paste those contents. You can try this (untested):
Code:
Sub LoopThroughDirectory()
Dim myBk As Workbook
Dim MyFile As String
Dim erow
Dim Filepath As String
Set myBk = ThisWorkbook
Filepath = "C:\Users\Guy\Desktop\nichola\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
    If MyFile = "ZMASTER.xlsm" Then
    Exit Sub
    End If
    
    Workbooks.Open (Filepath & MyFile)
    With Range("A2:E25")
        .Copy
        With myBk.ActiveSheet
            erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            .Paste Destination:=.Cells(erow, 1)
        End With
        .ClearContents
    End With
    ActiveWorkbook.Close savechanges:= True
    MyFile = Dir
Loop
End Sub



I get it now! That seems to have done the trick.

Thank you so much for your help.
 
Upvote 0

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