VBA - Sheet not calculating during macro

skiir10

New Member
Joined
Feb 3, 2018
Messages
7
I'm in need of a bit of help.

I am trying to run a Butterworth filter on a large number of separate files, then copy and paste into a different workbook select calculated values after filtering (Max, time to max, ...). I used this macro successfully once, but now need to apply a rolling average after the filtering. So I now have the raw values, run a butterworth filter vba, two columns of data that are calculated off of the filtered data, then a couple values that are calculated off of the previously mentioned two columns of data. The first time that worked was without the rolling average calculation (straight off filtered data). Now trying to get the values from the calculated rolling averages, it only pastes N/As and 0's. So it seems as though it is not calculating. If I do the same process manually it works fine. I have checked all of the normal solutions of various workbook calculations (manual v automatic) and changing the cell format to number.

Thanks in advance for the help.

Can send someone the excel file, just can't figure out how to post here.

Macro Below

Sub File_Loop_Example()

'Excel VBA code to loop through files in a folder with Excel VBA

Dim MyFolder As String
Dim MyFile As String

'Opens a file dialog box for user to select a folder

With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
MyFolder = .SelectedItems(1)
Err.Clear
End With

'stops screen updating, calculations, events, and statsu bar updates to help code run faster
'you'll be opening and closing many files so this will prevent your screen from displaying that

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'This section will loop through and open each file in the folder you selected
'and then close that file before opening the next file

MyFile = Dir(MyFolder & "", vbReadOnly)

Do While MyFile <> ""
DoEvents
On Error GoTo 0
Workbooks.Open FileName:=MyFolder & "" & MyFile, UpdateLinks:=False
''''''''''''ENTER YOUR CODE HERE TO DO SOMETHING'''''''''

Workbooks("Macros.xlsm").Activate
Rows("1:2").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(MyFile).Activate
Rows("1:1").Select
Selection.Insert Shift:=xlDown
ActiveSheet.Paste
Application.Run "butfilter"
Application.CutCopyMode = False
Calculate
Range("F2:K2").Select
Selection.Copy
Workbooks("Kinetic Variables.xlsx").Activate
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir
Loop

'turns settings back on that you turned off before looping folders

Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationManual

End Sub​
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
The last line of your code is wrong.
It shouldn't be:
Code:
[COLOR=#333333]Application.Calculation = xlCalculationManual[/COLOR]
but rather:
Code:
Application.Calculation = xlCalculationAutomatic
 
Upvote 0
Hi Joe,

Thanks for the correction. Unfortunately, this did not solve the root of my problem of none of the variables calculating before being copied and pasted.
 
Upvote 0
Hi Joe,

Thanks for the correction. Unfortunately, this did not solve the root of my problem of none of the variables calculating before being copied and pasted.

Why would they calculate as you are turning calculation off?
 
Upvote 0
I'm not super good with VBA, but I presumed it still worked because I have a calculation line in the middle of the code. Like I had mentioned, everything worked fine with calculating variables off of the filtered data (An Excel Add-in Macro). It was only since I added another column of the rolling average calculations that it has stopped working. So I know this macro works as is without the additional column.
 
Upvote 0
So are you saying that if you run the code below then Range("F2:K2") on sheets(1) Workbooks(MyFile) does not calculate?

If yes what happens if you change
Code:
.Calculate
to
Code:
Application.CalculateFull


Code:
Sub File_Loop_Example()

    'Excel VBA code to loop through files in a folder with Excel VBA

    Dim MyFolder As String
    Dim MyFile As String

    'Opens a file dialog box for user to select a folder

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        MyFolder = .SelectedItems(1)
        Err.Clear
    End With

    'stops screen updating, calculations, events, and statsu bar updates to help code run faster
    'you'll be opening and closing many files so this will prevent your screen from displaying that

    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    'This section will loop through and open each file in the folder you selected
    'and then close that file before opening the next file

    MyFile = Dir(MyFolder & "", vbReadOnly)

    Do While MyFile <> ""
        DoEvents
        On Error GoTo 0
        Workbooks.Open Filename:=MyFolder & "" & MyFile, UpdateLinks:=False
        ''''''''''''ENTER YOUR CODE HERE TO DO SOMETHING'''''''''

        Workbooks("Macros.xlsm").Activate
        Rows("1:2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Workbooks(MyFile).Activate
        Rows("1:1").Select
        Selection.Insert Shift:=xlDown
        ActiveSheet.Paste
        Application.Run "butfilter"
        Application.CutCopyMode = False
        Calculate
End Sub
 
Upvote 0
Maybe disable this code as a test to see if your macro works without it.

Code:
[COLOR=#333333]'stops screen updating, calculations, events, and statsu bar updates to help code run faster[/COLOR]
[COLOR=#333333]'you'll be opening and closing many files so this will prevent your screen from displaying that[/COLOR]

[COLOR=#333333]Application.ScreenUpdating = False[/COLOR]
[COLOR=#333333]Application.DisplayStatusBar = False[/COLOR]
[COLOR=#333333]Application.EnableEvents = False[/COLOR]
[COLOR=#333333]Application.Calculation = xlCalculationManual[/COLOR]


For Test:

Code:
[COLOR=#333333]'stops screen updating, calculations, events, and statsu bar updates to help code run faster[/COLOR]
[COLOR=#333333]'you'll be opening and closing many files so this will prevent your screen from displaying that[/COLOR]

[COLOR=#333333]' Application.ScreenUpdating = False[/COLOR]
[COLOR=#333333]' Application.DisplayStatusBar = False[/COLOR]
[COLOR=#333333]' Application.EnableEvents = False[/COLOR]
[COLOR=#333333]' Application.Calculation = xlCalculationManual

[/COLOR][COLOR=#333333]Application.ScreenUpdating = True[/COLOR]
[COLOR=#333333]Application.DisplayStatusBar = [/COLOR][COLOR=#333333]True[/COLOR]
[COLOR=#333333]Application.EnableEvents = [/COLOR][COLOR=#333333]True[/COLOR]
[COLOR=#333333]Application.Calculation = xlCalculationAutomatic[/COLOR]

After you sort out the basic macro function, you can go back and disable some of the above again to speed things up, though I recommend NOT disabling Application.EnableEvents unless you have a very specific reason.



 
Upvote 0
Thanks for the suggestions. The test with the screen updating actually made me realize the problem was what I was inserting and pasting and the fact that it was actually being pasted into each file, not the data being pasted into the main "Macro" sheet. I guess that's the punishment for copy and pasting someone else's code without fully understanding it!

Thanks for the suggestions anyway.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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