VBA - Paste data from a different workbook

Dan_

New Member
Joined
Sep 9, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all! I am fairly new to VBA, and I am having trouble making an automated macro to copy data from a separate file.

I have posted the current code below, but I should explain what is happening. The workbook that the macro is in is called Scorecard, and this is where the data needs to get pasted. In the 'Scorecard' workbook, I have a cell containing the link to the file that data needs to be copied from, and that link is called 'Data_wkbk'.

Then I open the file linked in the 'Data_wkbk' cell as read only.

The range that I am copying is dynamic. The range is ultimately the variable 'FRange', and I know that the range is correct because I used a MsgBox to show that the range is B2:BW51, which is what I expected. The last 2 lines of the code is where it errors out (Run-time error '9': Subscript out of range). Can anybody help me understand why this isn't copying and pasting the data?


VBA Code:
Sub RetreiveData()


Dim scorecard As Workbook
Set scorecard = ThisWorkbook
Dim data_wkbk As String
data_wkbk = Worksheets("Input").Range("data_wkbk").Value


Dim src As Workbook
Set src = Workbooks.Open(data_wkbk, True, True)


Dim SC_Input As String
SC_Input = src.Worksheets("Input").Range("SC_Input").Value


Dim FRange As String
FRange = "B2:BW" & SC_Input


src.Worksheets("Build SLIME Data").Range("FRange").Copy
scorecard.Worksheets("SLIME Set Data").Range("A6").PasteSpecial Paste:=xlPasteValues
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Have you tried replacing this line:
src.Worksheets("Build SLIME Data").Range("FRange").Copy

with this (where the quote marks around FRange are removed)?
src.Worksheets("Build SLIME Data").Range(FRange).Copy
 
Upvote 0
Have you tried replacing this line:
src.Worksheets("Build SLIME Data").Range("FRange").Copy

with this (where the quote marks around FRange are removed)?
src.Worksheets("Build SLIME Data").Range(FRange).Copy
I still get the same error.
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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