Macro works stepping through but not when run

Ordnassela

New Member
Joined
Jun 2, 2021
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Hey guys, fairly new to VBA and this forum. I'm running into some issues running my code-it seems like it only works when I press F8.

It does exactly what I need it to do, but when I press play, Excel freezes. Any thoughts on how I can fix it?

VBA Code:
Sub Depreciation1to5()
'
' This Macro is for Depreciating in FAR

Dim tDate As Date
Dim cel As Range
Dim rng As Range

'Enter latest month here
'#5/31/2021#
'#6/30/2021#
tDate = #4/30/2021#

'Filter for the last month
    Sheets("CUR FAR").Range("$A$1:$V$2474").AutoFilter Field:=20, Operator:= _
        xlFilterValues, Criteria2:=Array(1, tDate)

'Copy and Paste Special Value
    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 17).Select
    Range(Selection, Selection.End(xlDown)).Select
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    For Each cel In rng
    cel.Value = cel.Value
    Next cel
  
'Clear MO DEPRE Contents
'Click First Cell Filtered
    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 16).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    ActiveSheet.ShowAllData
  
'Change Column Q to Current Month 2021
 Range("S1").Select
    ActiveCell.FormulaR1C1 = "B-VALUE " & MonthName(Month(Now)) & " 2021"

'Filter for Yellow
 ActiveSheet.Range("$A$1:$V$2474").AutoFilter Field:=17, Criteria1:=RGB(255 _
        , 255, 204), Operator:=xlFilterCellColor
  
'Change Formula and Select Active Cell
'Change formula to *8
    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 17).Select
    Range(Selection, Selection.End(xlDown)).FormulaR1C1 = "=+RC[-1]*7"

End Sub
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

rlv01

Well-known Member
Joined
May 16, 2017
Messages
888
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Which line of code does it crash or freeze on?
 

Ordnassela

New Member
Joined
Jun 2, 2021
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Which line of code does it crash or freeze on?
It worked when I stepped through but once I pressed play Excel would freeze.

I figured it out though-simply kept ActiveSheet consistent throughout my code!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,333
Office Version
  1. 365
Platform
  1. Windows
Note that most of the time "Select" statements are unnecessary in VBA code, and you can speed up your code by combining lines that end in "Select" and the next line starts with "ActiveCell" or "Selection".

So this:
VBA Code:
 Range("S1").Select
    ActiveCell.FormulaR1C1 = "B-VALUE " & MonthName(Month(Now)) & " 2021"
can be simplified to this:
VBA Code:
 Range("S1") = "B-VALUE " & MonthName(Month(Now)) & " 2021"
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
888
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
If you just use Cells(BooBoo, "BP").Value instead of say Activesheet.Cells(BooBoo, "BP").Value you are relying on implicit references and relying on implicit references can get you into trouble if you are adding code snippets into other functions. For this reason it is good practice to get into the habit of alway using explicit references even if what you intend is to use ActiveSheet.
 

Ordnassela

New Member
Joined
Jun 2, 2021
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Note that most of the time "Select" statements are unnecessary in VBA code, and you can speed up your code by combining lines that end in "Select" and the next line starts with "ActiveCell" or "Selection".

So this:
VBA Code:
 Range("S1").Select
    ActiveCell.FormulaR1C1 = "B-VALUE " & MonthName(Month(Now)) & " 2021"
can be simplified to this:
VBA Code:
 Range("S1") = "B-VALUE " & MonthName(Month(Now)) & " 2021"
Noted! Thanks for this. I'll tweak my code :)
 

Forum statistics

Threads
1,136,732
Messages
5,677,432
Members
419,692
Latest member
Also_Confused

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
Top