Solved

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

Posted on 2014-11-12
4
149 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

679 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