Finance

Charts

Statistics

Macros

Search

Opening a File in Excel VBA

When displaying a file-open dialog, you can preset a default filename or even use wildcards as a filter.

Example:

Sub OpenFileDialog()
    Dim success As Boolean
    ' Show the built-in Excel "Open File" dialog with a preset filter
    success = Application.Dialogs(xlDialogOpen).Show(arg1:="C:\Users\POPOLY\Desktop\Doc\Document.txt")
    ' If the user cancels the dialog, show a message box
    If Not success Then MsgBox "No file was opened"
End Sub

Explanation:

  • The constant xlDialogOpen specifies the built-in Excel dialog for opening files.
  • The argument « C:\Users\POPOLY\Desktop\Doc\Document.txt » is a filter pattern: it shows only files whose names start with the letter « M » and have the .xlsx extension.
  • The user can override this filter and select any file.
  • After clicking Open, the selected file(s) are opened in Excel.
  • If the user clicks Cancel, the variable success is set to False, and a message box notifies the user that no file was opened.
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx