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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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