Solved

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

Posted on 2014-11-12
4
143 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 25

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 25

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now