Why does my VBA code jump to ActiveWorkbook.Save without running the rest of the code?

statiCat

New Member
Joined
Apr 21, 2018
Messages
19
I have a long macro that works great every time I run it except if I try to save at the end. If I put ActiveWorkbook.Save at the end it skips the code in front of it and just saves and does nothing else. What would cause this? I'm using the latest Excel and windows 10.
 
If you are using the 'bad' double quote characters then it is unlikely your code is running fine. For example used in this line
Code:
[COLOR=#574123]      Case "Sheet2Graph", “Sheet4Graph”
[/COLOR]

it means that “Sheet4Graph” is not being evaluate as a literal string, but instead as an empty variable. Let the compiler help you weed them out by putting this statement at the top of your code module
Code:
Option Explicit

Which will force all variables to be defined (which will help your coding in other ways as well)


The mismatched quotes were a result of copying/changing names/pasting into this forum. I have double checked and they are all the same in my code ("Sheet2Graph"). I have added Option Explicit to the top of my code module. The problem persists. When ActiveWorkbook.Save is added to the code, it skips right to it.
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
When ActiveWorkbook.Save is added to the code, it skips right to it.


What is being skipped? That is, where does the skipping begin and which lines of code are being skipped?
 
Upvote 0
What is being skipped? That is, where does the skipping begin and which lines of code are being skipped?

My code first check to see if the date is 7 days past the last row's date:

Code:
 [COLOR=black][FONT=Calibri]Sub Workbook_Refresher()[/FONT][/COLOR][COLOR=black][FONT='inherit']   
    ActiveWorkbook.Sheets("FirstSheet").Activate
    Dim lastRowDate As Date
    lastRowDate = ActiveSheet.Range("B458").Value
    
    If Date - lastRowDate >= 7 Then<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']       Dim ws As Worksheet
        For Each ws In Worksheets
            Select Casews.Name
               Case "FirstSheet", "ThirdSheet"               
                   Application.GoTo ws.Rows("459:459") 'select last row'
                   Selection.Delete
                   Application.GoTo ws.Range("M458")
                   Selection.Formula = "=L458/C458"
                                       
               Case "Sheet2Graph", "Sheet4Graph"
                    
                   Application.GoTo ws.Rows("4:4") 'select top row
                   Selection.Delete
                   Application.GoTo ws.Rows("59:59") 'select bottom row
                   Selection.Delete
    
            End Select
            
            Next ws<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']<o:p> </o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']    ForEach oWksht In ActiveWorkbook.Worksheets
        For Each oChart InoWksht.ChartObjects
            For EachmySrs In oChart.Chart.SeriesCollection
               mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, 57, 58)
            Next
        Next
    Next
            
    End If

[/FONT][/COLOR]


It isn't 7 days past until Friday so it's fine if it skips that part.

However, after the If statement, it should run this code regardless of what day it is:

Code:
[COLOR=black][FONT='inherit']<o:p> </o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']    ForEach oWksht In ActiveWorkbook.Worksheets
        For Each oChart InoWksht.ChartObjects
            For EachmySrs In oChart.Chart.SeriesCollection
               mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, 58, 58)
            Next
        Next
    Next<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']ActiveWorkbook.RefreshAll<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']ActiveWorkbook.Sheets("Sheet2Graph")Activate
Application.DisplayAlerts = False<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']
ActiveWorkbook.Save<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']    
End Sub<o:p></o:p>[/FONT][/COLOR]

That is what is being skipped. It does none of that when ActiveWorkbook.Save is in the code. I would know if it had done ActiveWorkbook.RefreshAll because there would be more data in the workbook.
 
Upvote 0
What is being skipped? That is, where does the skipping begin and which lines of code are being skipped?


After End If the code should run regardless of what day it is. So this is the part being skipped:
Code:
 [COLOR=black][FONT=Calibri]    ForEach oWksht In ActiveWorkbook.Worksheets[/FONT][/COLOR][COLOR=black][FONT='inherit']        For Each oChart InoWksht.ChartObjects
            For EachmySrs In oChart.Chart.SeriesCollection
               mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, 58, 58)
            Next
        Next
    Next<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']ActiveWorkbook.RefreshAll<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']ActiveWorkbook.Sheets("Sheet2Graph")Activate
Application.DisplayAlerts = False<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT='inherit']
[/FONT][/COLOR]

The next line is...

Code:
ActiveWorkbook.Save[/FONT][/COLOR][COLOR=black][FONT='inherit']
End Sub

<o:p></o:p>[/FONT][/COLOR]

...which it does.
 
Upvote 0
Put a break point on the RefreshAll line, then run the macro from the dialog. Does it stop on the Refreshall line?

My guess would be that you're refreshing some queries which run in the background and the save is interrupting them.
 
Upvote 0
If RoryA's theory turns out to be correct, try adding a time delay after the .RefreshAll statement:
Code:
     ForEach oWksht In ActiveWorkbook.Worksheets        For Each oChart InoWksht.ChartObjects
            For EachmySrs In oChart.Chart.SeriesCollection
               mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, 58, 58)
            Next
        Next
    Next
ActiveWorkbook.RefreshAll

WasteTime 5000  '<- adjust time delay downward via experimentation

ActiveWorkbook.Sheets("Sheet2Graph")Activate
Application.DisplayAlerts = False

The code for WasteTime is here. This is a routine I use a lot (not my code, but I don't remember exactly where I originally found it).
Code:
''' Simple delay (in milliseconds) procedure. Uses the Windows GetTickCount API
Sub WasteTime(DelayTimeInMS As Long)
    Dim T As Double
    Dim NowTick As Long
    Dim EndTick As Long

    EndTick = GetTickCount + (DelayTimeInMS)

    T = Timer
    Do
        NowTick = GetTickCount
        DoEvents
    Loop Until NowTick >= EndTick
End Sub


Needed declarations for WasteTime
Code:
Option Explicit

'Declarations  (these declarations go at the top of the module)
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]   Win64 Then
    '  Code is running in 64-bit version of Microsoft Office
    Public Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]  
    '  Code is running in 32-bit version of Microsoft Office
    Public Declare Function GetTickCount Lib "kernel32" () As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]   If
 
Upvote 0
Or amend the connection properties to not refresh in the background. :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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