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
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

807 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