• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Choosing a workbook that is already opened

matchlessman

New Member
Hi Everyone,

First post here from a beginner in VBA!

I'm creating a macro so that a user can choose a file to open in order to transfer its data into the active workbook.

If the chosen file is already opened, the warning message "Do you want to reopen ..." appears, and if the user selects "No", a Microsoft VB error message appears. In the event where the user selects "No", I want the macro to simply stop (close File Explorer without any error message appearing).

I've already searched the web and I've seen that Functions are generally used to solve this problem. However I have not been able to adapt a Function for my macro. Could someone help me out?

Code:
Dim wbContr As Workbook
Dim wbVerif As Workbook
Dim nmVerif As Variant

Sub ChooseFile()

Set wbContr = ActiveWorkbook

    'Open File Explorer to choose the workbook
    nmVerif = Application.GetOpenFilename

    'Exit File Explorer if user selects "Cancel" instead of "Open"
    If nmVerif = False Then End
    
    Set wbVerif = Workbooks.Open(nmVerif)
      
End Sub
Kind regards,
Maxwell
 
Last edited by a moderator:

Marc L

Excel Ninja
Hi !​
Apply a For Each loop on the Workbooks collection in order to compare each FullName property with the chosen file …​
 
Top