'Copying data from a CSV to and XLSX file using VBA in Excel 365
I am trying to create a macro that copies all the data from a csv file to the workbook that executed the macro.
I got the code below from this Youtube video
It appears to copy, but fails to paste.
I have tested this with .csv, .xlsx files; and combinations of sheet names and numbers.
I suspect it can't find the current workbook, because I have save the Marco to the Personal Macro file excel generates.
Can someone please advise how I can correct the paste location?
Sub AS_and_L_Reports()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("A1:P20").Copy
ThisWorkbook.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
OpenBook.Close True
End If
Application.ScreenUpdating = True
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|