Macro to select workboo/s to open

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro that let me select a workbook to open. I would like the macro to be amended to have a message box to ask if another file needs to be selected and if yes, then macro to start again , otherwise the macro must end

Code:
 Sub Open_Workbook()
    
    Dim A     As Variant
    
    ChDir "C:\My Documents"
    A = Application.GetOpenFilename
    If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
    
    With Workbooks.Open(A)
        With .Sheets("Imported Data")
            .Range("Q2", .Range("R" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)
        End With
        .Close SaveChanges:=False
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this:
Code:
Sub Open_Workbook()
    
[COLOR="#FF0000"]A: [/COLOR]   
Dim A     As Variant
    
    ChDir "C:\My Documents"
    A = Application.GetOpenFilename
    If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
    
    With Workbooks.Open(A)
        With .Sheets("Imported Data")
            .Range("Q2", .Range("R" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)
        End With
        .Close SaveChanges:=False
    End With
  [COLOR="#B22222"] answer = MsgBox("Does another file needs to be selected?", vbYesNo + vbQuestion, "Hello")
If answer = vbYes Then
GoTo A:
End If [/COLOR]  
 Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Thanks for the help, much appreciated
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top