copy data into the master from multiple files

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,286
Office Version
  1. 2013
Platform
  1. Windows
Code:
Application.ScreenUpdating = False
Worksheets("1000").Visible = True
Workbooks.Open Filename:="C:\SHARING FILES\xxxxxx\exports\1000.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("1000").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("1000.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("1000.xls").Activate
ActiveWindow.Close False
Worksheets("1000").Visible = False
ActiveWorkbook.Save
Application.ScreenUpdating = True

Good Day,
Is there any way to improve that code in regards if the needed file doesn't exist how can we disable to error notice.
I just trying to copy datas from closed workbooks with that code for each sheet of my master file...I beleive that im doing the wrong way but i have no choose to make it easier.
Destination folder has 12 files named with their created times(0600 to 0400 for every two hrs.)
How can i copy that files into my master file one by one from 0600 to 0400 with one click and also if there is missing file in it the code should ignore that and continue to finish the task after then i can load my userform accordingly.
Master file has 12 worksheets with the same file names.(0600 to 0400)
Many Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Application.ScreenUpdating = False
Worksheets("1000").Visible = True
Workbooks.Open Filename:="C:\SHARING FILES\xxxxxx\exports\1000.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("1000").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("1000.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("1000.xls").Activate
ActiveWindow.Close False
Worksheets("1000").Visible = False
ActiveWorkbook.Save
Application.ScreenUpdating = True

Good Day,
Is there any way to improve that code in regards if the needed file doesn't exist how can we disable to error notice.
I just trying to copy datas from closed workbooks with that code for each sheet of my master file...I beleive that im doing the wrong way but i have no choose to make it easier.
Destination folder has 12 files named with their created times(0600 to 0400 for every two hrs.)
How can i copy that files into my master file one by one from 0600 to 0400 with one click and also if there is missing file in it the code should ignore that and continue to finish the task after then i can load my userform accordingly.
Master file has 12 worksheets with the same file names.(0600 to 0400)
Many Thanks


Code:
Application.ScreenUpdating = False
Workbooks.Open Filename:="C:\SHARING FILES\me\exports\0800.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("0800").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("0800.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("0800.xls").Activate
ActiveWindow.Close False
Workbooks.Open Filename:="C:\SHARING FILES\me\exports\1000.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("1000").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("1000.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("1000.xls").Activate
ActiveWindow.Close False
Workbooks.Open Filename:="C:\SHARING FILES\me\exports\1200.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("1200").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("1200.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("1200.xls").Activate
ActiveWindow.Close False
Workbooks.Open Filename:="C:\SHARING FILES\me\exports\1400.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("1400").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("1400.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("1400.xls").Activate
ActiveWindow.Close False
Application.ScreenUpdating = True


Actually this is the way im using for to copy the closed workbook sheets into my master file.
The only one think i need to improve that ,if there is missing file it does not work ...what i need how can i disable to "Debug" msj box then it will be continiou untill the end of code.It doesn't matter will be copy all files or not.
Many Thanks.
 
Upvote 0
Place this function in your Module
Code:
Function FileExists(ByVal strPath As String) As Boolean
'Function returns true if file exists, false otherwise
    If Dir(strPath) > "" Then
        FileExists = True
    Else
        FileExists = False
    End If
End Function

then for each file

Code:
If FileExists("C:\SHARING FILES\me\exports\1000.xls") Then
Workbooks.Open Filename:="C:\SHARING FILES\me\exports\1000.xls"
Cells.Select
Selection.Copy
Windows("result.xls").Activate
Sheets("1000").Select
Cells.Select
ActiveSheet.Paste
Cells.EntireColumn.AutoFit
Sheets("Sheet1").Select
Windows("1000.xls").Activate
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("1000.xls").Activate
ActiveWindow.Close False
End If
 
Upvote 0
Code:
Option Explicit
Sub getCounts()
Dim wbA As Workbook, wbB As Workbook
Dim a As Range
Set wbB = ThisWorkbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set wbA = Workbooks.Open(Filename:="C:\SHARING FILES\me\exports\0800.xls")
Set a = wbA.Worksheets(1).Range("A:Q").CurrentRegion
Selection.Copy
Windows("Result.xls").Activate
Sheets("0800").Select
wbB.Sheets("0800").Columns("A:A").Select
ActiveSheet.Paste
Range("A1").Select
Sheet1.Select
wbA.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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