VBA - GetOpenFilename

Dao Ha Quang

New Member
Joined
Apr 30, 2023
Messages
20
Office Version
  1. 2016
Sub CopySheetFromClosedWB()
Application.ScreenUpdating = False
Filename = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm")
Set closedBook = Workbooks.Open(Filename)

closedBook.Sheets("Sheet1").Move Before:=ThisWorkbook.Sheets("TT")

closedBook.Close SaveChanges:=False
Application.ScreenUpdating = True

Call Macro2

End Sub

I use this code to copy 1 sheet from 1 closed file and then run "MACRO 2"
When I run it, it OPEN the file and I select the file, marco still runs normally,
but when I don't select file and CANCEL, my "MACRO 2" is still running and it crashes
I want "MACRO 2" to run only after I have selected OPEN a file
Thank you.
1682857053948.png
 

Attachments

  • 1682857004585.png
    1682857004585.png
    19.5 KB · Views: 9

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi @Dao Ha Quang.
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.​

Add the line highlighted in blue:


Rich (BB code):
Sub CopySheetFromClosedWB()
  Dim Filename As Variant
  Dim closedBook As Workbook
 
  Application.ScreenUpdating = False
  Filename = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm")
 
  If Filename = False Then Exit Sub
 
  Set closedBook = Workbooks.Open(Filename)
 
  closedBook.Sheets("Sheet1").Move Before:=ThisWorkbook.Sheets("TT")
 
  closedBook.Close SaveChanges:=False
  Application.ScreenUpdating = True
 
  Call Macro2

End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Solution

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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