script runs when manually stepping through it, but not otherwise

rrands1

New Member
Joined
Nov 7, 2004
Messages
29
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I am having an issue that I *think* is related to timing. I have the following code, where I have inserted a couple of waits (to try and troubleshoot). If I step through it (i.e. set a breakpoint at the beginning of the sub & then step through it with F8), it works fine. Otherwise, if I just try and run through it at full speed, it just seems to end the sub w/o any messages or anything. In the example below, when running at full speed, it never presents the "wait 2 done" message box - it just quits processing. Any thoughts or tips on running this down?

VBA Code:
Dim SourceWB As Workbook ' holds name of Source workbook (Coles) for easier reference
Dim TargetWB As Workbook ' holds name of Target workbook (County Export) for easier reference
Dim ssheet As Worksheet ' sheet1 in the source (Coles) workbook (to make coding shorter)
Dim tsheet As Worksheet  'sheet1 in the target (County) workbook (to make coding shorter)

Dim Sourcelastrow ' holds NUMBER of last row with Data from Source sheet
Dim Targetlastrow ' holds NUMBER of last row with Data from Target sheet

Dim targetfilepath As String

Dim L As Integer ' length of a cell's contents
Sub Add_Coles()
' integrates phone number (and Name) information from a COLES directory export into the County export

' prompt user to pick exported file from Coles directory
Set xfdCOLESexp = Application.FileDialog(msoFileDialogFilePicker)
    xfdCOLESexp.Title = "Choose Coles Export file to add the phone numbers from:"
    If xfdCOLESexp.Show = -1 Then
        xFdCOLESItem = xfdCOLESexp.SelectedItems(1)
        '     MsgBox ("Dir:  " & xFdCOLESItem)
    Else
        MsgBox ("You cancelled - terminating process - you will need to start over.")
        End
    End If

targetfilepath = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
    
Application.Wait Now + TimeValue("00:00:05")
    MsgBox ("wait 1 done")
Set SourceWB = Workbooks.Open(xFdCOLESItem)
Application.Wait Now + TimeValue("00:00:03")
    MsgBox ("wait 2 done")
Set ssheet = SourceWB.Sheets(1) ' sheet1 in the COLES workbook (to make coding shorter)
Set TargetWB = Workbooks.Open(targetfilepath)
Set tsheet = TargetWB.Sheets(1) ' sheet1 in the County export workbook (to make coding shorter)



Thank you for any help - I am totally stuck!!
 
Nice! And weird
... nevertheless explainable.

It's not a bug. After all, opening a workbook (from within Excel) with the Shift key pressed disables all macros, regardless whether it's done manually or through VBA***.
In previous versions of Excel, when there was no Trust Center, this way any workbook could be opened safely without executing any Workbook_Open() or Auto_Open() event handler, and it still can.
So bottom line: don't use the Shift key in your shortcut when the invoked process is supposed to open a workbook (or any other file using the Application.Workbooks.Open method).

*** When it's done through VBA the Shift key's state at the start of any process is decisive and not the moment when a workbook is actually opened. So it's no use in implementing a small delay or even displaying a message box.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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