Opening all files in a folder and pasting new dat from source workbook

AndyXL

New Member
Joined
Oct 28, 2004
Messages
9
I have a monthly process wher I have to update data in around 20 spreadsheets. I have found and successfully used code to open all the files but cant seem to get the copy and paste part to work.
Here is my code so far.

Sub UpdateAllSandbox()
Dim FileName, folderpath, FileArray(), period As String
Dim Count1, i As Integer
Dim SourceWB, DestWB As Workbook
period = "P" & Range("N6").Value & "-2020"

folderpath = "C:\Users\ahall\NDC Technologies\SEC-Finance - Documents\2020\Commissions\CalculatorsTEST\" & period & "\"

If Right(folderpath, 1) <> "\" Then
folderpath = folderpath & "\"
End If

FileName = Dir(folderpath & "*.xlsm")

Count1 = 0

While FileName <> ""
Count1 = Count1 + 1
ReDim Preserve FileArray(1 To Count1)
FileArray(Count1) = FileName
FileName = Dir()
Wend

Set SourceWB = ThisWorkbook

For i = 1 To UBound(FileArray)

Set DestWB = Workbooks.Open(folderpath & FileArray(i))

' this is the bit I cant get to work

' DestWB.Worksheets("YTD Orders").Visible = xlSheetVisible
' SourceWB.Worksheets("OrderData").Range("A:R").Copy
' DestWB.Worksheets("YTD Orders").Range("A:R").Paste
'DestWB.Worksheets("Lists").Visible = xlSheetHidden
'DestWB.Close True


Next

Set DestWB = Nothing
Set SourceWB = Nothing

End Sub




Any help as always gratefully received
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Replace this:
VBA Code:
SourceWB.Worksheets("OrderData").Range("A:R").Copy
 DestWB.Worksheets("YTD Orders").Range("A:R").Paste

With this"

VBA Code:
 SourceWB.Worksheets("OrderData").Range("A:R").Copy DestWB.Worksheets("YTD Orders").Range("A1")
 
Upvote 0
Thank you that worked a treat, I did run into one issue though, if there is a filter applied to destination worksheet the data does not paste correctly, is there a command to clear any filters if they exist.?

Thanks again for your help.
 
Upvote 0
Thank you that worked a treat, I did run into one issue though, if there is a filter applied to destination worksheet the data does not paste correctly, is there a command to clear any filters if they exist.?

Thanks again for your help.

You can try this. Put it before the copy statement.
VBA Code:
DestWB.Worksheets("YTD Orders").AutoFilterMode = False
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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