Help With Copy and Paste Macro

welshraz

New Member
Joined
Apr 29, 2016
Messages
40
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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