Copy, Paste and Close File Macro

LightningBolt

New Member
Joined
Nov 10, 2011
Messages
43
Hey,

First time posting here and I'm hoping somebody can be of some help. What I'm looking to do is above my excel skills at the moment.

I've got 200 odd workbooks that are excel versions of system reports. What I want to do is to take data from each workbook and paste it into a brand new "master" workbook so that I can run pivots off it. I tried to record a macro to do this as follows:

1) First I copy the data I want from each workbook that I want copied in (lets call it Workbook 1)
2) I then select "Record Macro" and pasted it into Workbook Master.
3) I then close Workbook 1 and finish recording.
4) I open up Workbook 2 and select the data again I want copied and try to run the macro but an error message shows up.

Ideally the macro could do the following in workbook 1 (i.e. the open workbook)

1) Select and copy all data in the rows from Cell A9 (Cell A8 always has the following text "Opening Balance" to the row just before a cell in Column A says "Closing Balance") - Some reports have maybe 10 rows of data, others could have 50 rows of data.
2) The data selected will be pasted into the Master Workbook in cell A2.
3) Workbook 1 will then close.
4) When I then open Workbook 2 I would like addiitonal data to be pasted in the next empty row.
C
an anybody please advise?

Many thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Here is my interpretation of the solution to your request

The files to be copied are listed in the “Master” workbook in a sheet called “Files” in column A. If the files are not in the default directory, they must include the full path in the file name i.e. C:\temp\abc.xls

To start the process:

  • Open the “Master”
  • Copy these two subs to a module in “Master”
  • Create a sheet named “Files”
  • Put in the names of the files to be copied in “Files” column A
  • Run GetFiles


Sub copyrows(swbname)
If LCase(Right(swbname, 4)) <> ".xls" Then swbname = swbname & ".xls"
On Local Error Resume Next
Set x = Workbooks.Open(Filename:=swbname)
If IsEmpty(x) Then
MsgBox Err.Description
Err.Clear
Exit Sub
End If
Windows(swbname).Activate
istart = 10
iend = Range("A" & Rows.Count).End(xlUp).Row - 1
Rows(istart & ":" & iend).Copy
Range("A9").Select
Windows("Master 1.xls").Activate
istart = WorksheetFunction.CountA(Range("A:A")) + 2
If istart = 1 Then istart = 2
ActiveSheet.Range("A" & istart).Select
ActiveSheet.Paste
ActiveSheet.Range("A2").Select
Application.DisplayAlerts = False
Windows(swbname).Close False
Application.DisplayAlerts = True
End Sub

Sub GetFiles()
Application.ScreenUpdating = False
i = 0
Do While Sheets("Files").Range("A1").Offset(i, 0).Text <> ""
copyrows Sheets("Files").Range("A1").Offset(i, 0).Text
i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,607
Messages
6,056,284
Members
444,855
Latest member
archadiel

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