Macro Causes Excel to Not respond

Jared_Jones_23

New Member
Joined
Jun 24, 2011
Messages
34
Hello,
I am relatively new to VBA and I have written a macro code that searches certain cells and then puts a formula into different cells that match the criteria. My macro is searching through thousands of cells and gets hung up and freezes whenever its run. Here the part of the code that is getting the error. If anyone has any suggestions I would appreciate it.
Thank You
Jared

Private Sub Workbook_Open()
Dim lastRow, lastCol As Long
Dim i, j As Integer

With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With

lastCol = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.count
lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.count
count = 0
msg = "Work"

For i = 20 To lastRow
For j = 53 To lastCol
If Cells(i, j) <= Cells(17, j) And Cells(i, 14) >= Cells(18, j) Then
Cells(i, j) = (Cells(18, j) - Cells(17, j) + 1) / (Cells(i, 14) - Cells(i, 13) + 1) * Cells(i, 18) * Cells(i, 20)
End If
Next j
Next i

With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello and Welcome,

One thing that might help is to work with the data range instead of referencing the UsedRange Property.

UsedRange can be much larger and include cells have formatting but no data, or cells that had values that were deleted long ago.

Try replacing:
Code:
lastCol = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count

With this:
Code:
lastCol = Cells.Find(What:="*", After:=Range("A1"), _
    SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
lastRow = Cells.Find(What:="*", After:=Range("A1"), _
    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

There are a few other things that are of concern, but I don't want overwhelm you with feedback on your first post here! :)
 
Upvote 0

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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