Excel Workbook Closes after running Macro

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro to open a workbook and then to fo to a named cell on a sheet

However when running the macro, the workbook closes

If I manually open the soource workbook and then run the macro, The workbook does not close. I suspect that this portion of the code is causing the issue

Code:
 Windows("STATS BR ales.xls").Activate
 Sheets("Sales Acc Numbers").Select
Application.Goto Reference:="Sales"


Code:
 Sub Open_SalesFile()
    
   
ChDir "C:\My Documents"
Dim A As Variant
Dim LR As Long

MsgBox "Select Current Years Sales File"

If TypeName(A) = "Boolean" Then Exit Sub


Dim FileAry As Variant, Fle As Variant
   With Application.FileDialog(3)
      .InitialFileName = "C:\My Documents\"
      .AllowMultiSelect = True
      .InitialFileName = "Sales.*.xls*"
      If .Show Then Set FileAry = .SelectedItems()
   End With
   For Each File In FileAry
   If TypeName(A) = "Boolean" Then Exit Sub

   
      With Workbooks.Open(File)
   
     


End With
Next
Windows("STATS BR ales.xls").Activate
 Sheets("Sales Acc Numbers").Select
Application.Goto Reference:="Sales"

End Sub


It would be apreciated if someone could amend my code to prevent file closing down unexpectently
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi howard,

A is dimmed as Type Variant but no value is set. So both the checks inside your sniplet are obsolete. Maybe check your code and cancel when the Dialog is displayed...

Your description about selecting a range is not clear to me: inside each opened workbook or only at the end?

VBA Code:
Sub Open_SalesFile()
' https://www.mrexcel.com/board/threads/excel-workbook-clolses-after-running-macro.1228098/
Dim FileAry As Variant
Dim Fle As Variant

ChDir "C:\My Documents"

MsgBox "Select Current Years Sales File"

With Application.FileDialog(3)
  .InitialFileName = "C:\My Documents\"
  .AllowMultiSelect = True
  .InitialFileName = "Sales.*.xls*"
  If .Show <> 0 Then
    Set FileAry = .SelectedItems()
  Else
    Exit Sub
  End If
End With
For Each Fle In FileAry
  With Workbooks.Open(Fle)
    On Error Resume Next
    Application.Goto Reference:="Sales"
    Err.Clear
    On Error GoTo 0
  End With
Next Fle
Workbooks("STATS BR ales.xls").Activate
'Sheets("Sales Acc Numbers").Select
Application.Goto Reference:="Sales"

End Sub

Ciao,
Holger
 
Upvote 0
Thanks for the hrlp Holger

The range must only be seelected at the end
 
Upvote 0
Hi howard,

instead of
VBA Code:
For Each Fle In FileAry
  With Workbooks.Open(Fle)
    On Error Resume Next
    Application.Goto Reference:="Sales"
    Err.Clear
    On Error GoTo 0
  End With
Next Fle
use
VBA Code:
For Each Fle In FileAry
  Workbooks.Open (Fle)
Next Fle
If there is only one range in "STATS BR ales.xls" named "Sales" then you must only activate that workbook and the proper cell will be selected (no need to select the sheet then).

Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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