Extract data without openning (msoFileDialogFilePicker) the file on the workbook only if it's an Excel type file

Gwhaou

Board Regular
Joined
May 10, 2022
Messages
78
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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 :

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 🙏
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You can add filters like this:

VBA Code:
With Application.FileDialog(msoFileDialogFilePicker)
   .Filters.Add "Excel workbooks", "*.xls*", 1
 
Upvote 0
You can add filters like this:

VBA Code:
With Application.FileDialog(msoFileDialogFilePicker)
   .Filters.Add "Excel workbooks", "*.xls*", 1
Sorry for the late reply, I have already tried that, the problem is the user can modify the type of file and put All type off file.
I want want to know if it's possible of create a if condition that tells if the file selected by msofildialogfilePicker is different of .xls (if ....... <> Format ".xls" then exit sub Msgbox("......")) then exit the sub with a message telling is not the right format

I think I'm asking a complexe thing 😅
 
Upvote 0
the problem is the user can modify the type of file and put All type off file
That shouldn't be possible unless you previously added an all filter. Try:

VBA Code:
With Application.FileDialog(msoFileDialogFilePicker)
   .Filters.Clear
   .Filters.Add "Excel workbooks", "*.xls*", 1
 
Upvote 0
Solution

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top