Hello,
I need your, help actually i'm using this code to allow the user to extract data from a unopened excel file,
The macro will open a window to allow the user to select the file that he wants to copy data on the actual workbook :
I need some help because, the code works fine when the user choose an excel file (when the msofiledialogfilepicker opens) but when we open a different file there is an error.
I want to know if it's possible to restrict the type of the file (specify only excel file), if it's different than an excel file exit the sub with message (Pleaser open a excel file)
I would appreciate your help
I need your, help actually i'm using this code to allow the user to extract data from a unopened excel file,
The macro will open a window to allow the user to select the file that he wants to copy data on the actual workbook :
VBA Code:
Sub Extract()
Dim Wrkb_Org As Workbook 'Variable to link the base workbook (which is supposed to be unopen)
Dim Wrkb_Trs As Workbook 'Variable for the actual workbook
Dim Destination As String
Dim Last_line As Long
With Application.FileDialog(msoFileDialogFilePicker)
If .Show <> 0 Then
'Opens the selected file and select the first sheet
'Activate the
Destination = .SelectedItems(1)
Set Wrkb_Org = Workbooks.Open(Destination)
Set Wrkb_Trs = Workbooks("Template_file.xlsm")
Wrkb_Org.Sheets(1).Activate
'Find the last line to copy all the data from the unopen workbook first sheet from column A to C and copy that
Last_line = Wrkb_Org.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Wrkb_Org.Sheets(1).Range("A1:CJ" & Last_line).Copy
'Copy all the data and paste it on the actual workbook from the first cell
Wrkb_Trs.Sheets("Sheet1").Activate
Range("A1").Select
ActiveSheet.Paste
'when it as been copied close and save it
Application.CutCopyMode = False
Wrkb_Org.Sheets(1).AutoFilterMode = False
Wrkb_Org.Save
Wrkb_Org.Close
MsgBox (" Data has been transfered")
End Sub
I need some help because, the code works fine when the user choose an excel file (when the msofiledialogfilepicker opens) but when we open a different file there is an error.
I want to know if it's possible to restrict the type of the file (specify only excel file), if it's different than an excel file exit the sub with message (Pleaser open a excel file)
I would appreciate your help