Help With Copy and Paste Macro

welshraz

New Member
Joined
Apr 29, 2016
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to create a macro that copies and pastes all the data from one tab in a closed workbook into an open and active one. Two main considerations: the name of the source file changes every day and the amount of data with vary each day (some days one line of data, other times 10,000+).

I have set up an 'Inputs' tab so that I can manually adjust the source file name each day, with a button linked to the macro to run it. I have used this for other macros and has worked fine. This is what I have so far:

Sub PullClosedData()
Dim filePath_1 As String
Dim SourceWb_1 As Workbook
Dim TargetWb As Workbook


Set TargetWb = ActiveWorkbook

i = 5 'row number of first input sheet in "Inputs" tab

'Tab 16: Data - NonTask

filePath_1 = TargetWb.Sheets("Inputs").Range("B" & i).Value

Set SourceWb_1 = Workbooks.Open(filePath_1)

'Transfer values from x to y:
TargetWb.Sheets("Data - Non Task").Range("A1").Value = SourceWb_1.Sheets("Data - Non Task").Range("A1")
With SourceWb_1.Sheets("Data - Non Task").UsedRange

'Now, paste to y worksheet:
TargetWb.Sheets("Data - Non Task").Range("A1").Resize( _
.Rows.Count, .Columns.Count) = .Value

End With

SourceWb_1.Close

End Sub

I keep getting a 'Subscript out of range' error but no option to debug, so I have no idea where I'm going wrong. The source workbook opens so at least I'm getting that far.

I have absolutely no idea what I am doing as I'm very new to this. Please be kind!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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