Solved

What error handling should I use for an Excel VBA GetOpenFileName method

Posted on 2014-11-12
4
152 Views
Last Modified: 2014-11-12
Hi Experts,
I have an Excel application which displays the Open file dialog box. However, if I select the Cancel button my application falls over. What should I be testing for in this instance ? How do I code it ?

Thanks
Toco
0
Comment
Question by:Tocogroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Expert Comment

by:ProfessorJimJam
ID: 40436862
put this before

on error goto next

then after the code line put this  on error goto 0
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40436864
Hi, you can use this code :

Dim select_file As String
select_file = Application.GetOpenFilename("All Files,.", 1, "Choose file", , False)

If select_file = "False" Then
    Exit Sub
End If

Open in new window

0
 
LVL 26

Expert Comment

by:ProfessorJimJam
ID: 40436896
here it is

Sub OpenFile()

' by ProfessorJimJam
FFF = MsgBox("Do you know where your file is located?", vbYesNo, "Do you know where your file is located")
'MsgBox (FFF)
If FFF = vbNo Then Exit Sub
If FFF = vbYes Then

With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Only EXCEL FILE *.xlsx can be selected", "*.xlsx", 1

        
        .Show
           For lngCount = 1 To .SelectedItems.Count
            PathPath = .SelectedItems(lngCount)
        Next lngCount
End With

If Len(PathPath) = 0 Then
FFF = MsgBox("You have CANCELLED selection of needed FILE", vbCritical, "- FOLLOW INSTRUCTION")
Exit Sub

'rest of your code

end sub

Open in new window

0
 

Author Closing Comment

by:Tocogroup
ID: 40436939
Thank you for your suggestions, all.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question