subscript out of range - file dialogue

Kaps_mr2

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

I am trying to open a file dialogue box in Excel but keep getting " runtime Error 9 - subscript out of range" on the line in bold. Any thoughts ? Thanks

Kaps

Rich (BB code):
Public Sub Get_source_data()
Dim file_name As String

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

'open the Sage CSV file


Set fd2 = Application.FileDialog(msoFileDialogFilePicker)


With fd2
    .AllowMultiSelect = False
   ' .InitialFileName = local_path
    .Title = "Please select price file"
     .Filters.Add "Text Files", "*.csv", 2
    .FilterIndex = 2
    If .Show = -1 Then file_name = .SelectedItems(1)
 End With

If Len(file_name) > 0 Then
    
    
    'open the workbook
    
    Workbooks.Open file_name
    Set price_workbook = ActiveWorkbook
    
    With ActiveSheet
    .Name = "Sheet1"
    End With
    
    Sheets.Add
    
    With ActiveSheet
    .Name = "graph"
    End With
    
    
    
End If

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
It is because there are no filters in the first place so the computer cannot find index 2.

Moreover, you might want to add .Filters.Clear before adding the filters or else the program will have LOTS of filters when run repeatedly.
 
Upvote 0
You've only got one filter so you can't specify 2 as the position.
 
Upvote 0
Thanks - to both of you for this. Implemented both ideas and now fixed.

regards

kaps
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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