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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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")
 

AndyXL

New Member
Joined
Oct 28, 2004
Messages
9
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.
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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
 

Forum statistics

Threads
1,136,290
Messages
5,674,886
Members
419,532
Latest member
longphamtel

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
Top