User Peh gave me great code to copy data from a closed workbook to an open workbook, but now I want to do the reverse. I want to have an open workbook with formula and values I want to copy paste these as values into a closed workbook. I want to copy range B36:K36
from the "AllData" tab in my open workbook to paste values starting in K1
in Sheet1 of a closed workbook called "archive.xlsx".
This is my current code (from the internet):
Sub CopynPasteWrkBk()
Dim InputFile As Workbook
Dim OutputFile As Workbook
Dim Inputpath As String
Dim Outputpath As String '
' Set path for Input & Output
fileInputpath = "/Users/cie/Desktop/macrotest/"
Outputpath = "/Users/cie/Desktop/macrotest/"
'## Open both workbooks first:
Set InputFile = ActiveWorkbook
Set OutputFile = Workbooks.Open(Outputpath & "archive.xlsx")
'Now, copy what you want from InputFile:
InputFile.Sheets(“AllData”).Activate
InputFile.Sheets(“AllData”).Range("B36:K36").Copy
'Now, paste to OutputFile worksheet:
OutputFile.Sheets("Sheet1").Activate
OutputFile.Sheets("Sheet1").Range("k1").PasteSpecialOutputFile.Save
'Close InputFile & OutputFile:
InputFile.Close
OutputFile.Close
End Sub
When I execute this code in my active workbook, it successfully opens up "archive.xlsx", gives me a runtime error 9 subscript out of range
and errors out, then I see that it has selected selects K1
in my active workbook (which is the cell in which I want to start pasting in the "archive.xlsx" workbook). What is wrong? Both files live in the same folder on my desktop.
“AllData”
, but you should tell us so that we don't have to all guess (and all guess something different). – YowE3K