I'm running an Excel Macro that is store in the active workbook. I want it to prompt to open a file, referenced as OldWorkbook and copy values from OldWorkbook then paste their values into the active workbook.
I'm having errors after the 'Copy range to clipboard note. It's like it doesn't recognize any value to "OldWorkbook" at this point?
Sub Version_Convert()
Dim OldWorkbook As Variant
OldWorkbook = Application.GetOpenFilename("Excel Files (*.xl*),*.xl*", , "Choose File", "Open", False)
If OldWorkbook = "False" Then
Else
Workbooks.Open (OldWorkbook)
End If
'Copy range to clipboard
Workbooks(OldWorkbook).Worksheets("PKG").Range("B12:CW28").Copy
'PasteSpecial to paste values, formulas, formats, etc.
ThisWorkbook.Worksheets("PKG").Range("B12:CW28").PasteSpecial Paste:=xlPasteValues
End Sub
Why is using the string giving me an error? How do I make OldWorkbook usable?
Referencing and Copying
Application.GetOpenFilenamewill return either the (full) path of a file (String) orFalse(Boolean). That's why you have to declareOldWorkbookas aVariantin the first place. Therefore it is incorrect to use the quotes ("False").Application.GetOpenFilenameisC:\Test\Test.xlsx.In your code, you are actually trying to do
Workbooks("C:\Test\Test.xlsx").Worksheets("PKG").Range("B12:CW28").Copy(wrong) instead ofWorkbooks("Test.xlsx").Worksheets("PKG").Range("B12:CW28").Copy(correct), becauseWorkbooks(...)needs the file name, whileWorkbooks.Open(...)needs the file path.PasteSpecial(see solutions 2 and 3).