file dialogue box to open CSV file

Kaps_mr2

Well-known Member
Joined
Jul 5, 2008
Messages
1,583
Office Version
  1. 365
Platform
  1. Windows
Dear All,

I am trying to use the following code to open first an Excel file and then a CSV file. the Excel filter is fine. However I cannot get the filter to show only CSV Files (see the line in bold which ought to restrict the view to just CSV files - bu gives me Excel files). Thanks

Kaps

Rich (BB code):
Dim file_name As String

Dim FilterIndex As Integer
Dim filter As String
Dim fd1 As FileDialog
Dim fd2 As FileDialog


'open the credit card file

Set fd1 = Application.FileDialog(msoFileDialogFilePicker)


With fd1
    .AllowMultiSelect = False
   ' .InitialFileName = local_path
    .Title = "Please select Bank file"
    .Filters.Add "Excel files", "*.xls; *.xlsx", 1
    If .Show = -1 Then file_name = .SelectedItems(1)
End With

If Len(file_name) > 0 Then
    
    
    'open the workbook
    
    Workbooks.Open file_name
    Set Credit_card_workbook = ActiveWorkbook
    
    
End If


'open the Sage CSV file


Set fd2 = Application.FileDialog(msoFileDialogFilePicker)


With fd2
    .AllowMultiSelect = False
   ' .InitialFileName = local_path
    .Title = "Please select Sage file"
    .Filters.Add "text files| ", "*.csv"
    If .Show = -1 Then file_name = .SelectedItems(1)
End With

If Len(file_name) > 0 Then
    
    
    'open the workbook
    
    Workbooks.Open file_name
    Set Sage_Input_workbook = ActiveWorkbook
    
    
End If
thanks

Kaps
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you click on the drop down menu located at the bottom right of the dialog box (should be just above the 'Open' and 'Cancel' buttons), the 'csv' filter should have been added. You simply have to select it in order to bring up all 'csv' files. However, if you'd like to set the default filter to 'csv', try replacing...

Code:
[B].Filters.Add "text files| ", "*.csv"[/B]

with

Code:
.Filters.Add "Text Files", "*.csv", 2
.FilterIndex = 2
 
Upvote 0
Or put:
Code:
.Filters.CLear
before the Add if you only want the csv files available.
 
Upvote 0
Thanks - Used Domenic's solution and it worked.

RoryA thanks for your input - always appreciated.

Kind Regards

Kaps
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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