PDA

View Full Version : Macro To Browse&Select File and import Specific Data



madeinnorway
09-20-2019, 01:24 AM
Hi guys!

I am trying to run a macro that will let me select a file for import, and then export specific data to my active spreadsheet.

This is the code I am using:

Sub copy_sheet()
Dim ws As Worksheet
Dim ns As Worksheet
Dim lr As Long

Set ws = Sheets("D.Utregning")

MyPath = Application.ActiveWorkbook.Path
ChDrive MyPath
ChDir MyPath

MsgBox "Select File"
'prompt the user to open xls, xlsx or csv file to import
strFileToOpen = Application.GetOpenFilename _
(Title:="Please choose file to import", _
FileFilter:="Excel Files *.xls*; *.csv (*.xls*; *.csv),")

If strFileToOpen = False Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Workbooks.Open FileName:=strFileToOpen
End If

Set ns = ActiveSheet

lr = ns.Cells(Rows.Count, "A").End(xlUp).Row
ns.Range("A1:M6000" & lr).Copy

ws.Range("A1").PasteSpecial xlPasteAll
ns.Select
ActiveWorkbook.Close
End Sub


I am getting this error:
https://prnt.sc/p8et9r
https://prnt.sc/p8etns

I am using newest ver. of Excel for Mac (Office365).

Would appreciate it if anyone could point me in the right direction :)