Small Macro running slow

Stephenosn

Board Regular
Joined
Jun 2, 2015
Messages
52
Hello all,

I'm having trouble with a small micro running much slower than I believe it should. Please see the code bellow.

Code:
Sub NBill_Click()

Dim NBill As Integer
    NBill = MsgBox("This action will update the previous percent complete and the total billed to date?", _
    vbOKCancel, "NEW BILLING PERIOD?")
    If NBill = vbCancel Then
        Exit Sub
    End If
     
    'Add Amount to bill this period to total amount billed to-date
    Dim ABill As Object
          For Each ABill In Range("J5:J60")
              ABill.Value = ABill.Offset(0, 0).Value _
                  + ABill.Offset(0, -1).Value
          Next
'Replace previous percent complete with percent completed to-Date
    Dim rng As Range
        Set rng = Worksheets("SOV").Range("H5:H60")
            Worksheets("SOV").Range("F5").Resize _
            (rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
End Sub

I have walked through the code and it appears the problem is with the following procedure.

Code:
'Add Amount to bill this period to total amount billed to-date    Dim ABill As Object
          For Each ABill In Range("J5:J60")
              ABill.Value = ABill.Offset(0, 0).Value _
                  + ABill.Offset(0, -1).Value
          Next

Please let me know if there is something I'm missing here that would speed this macro up or a better way to write the code.

Thank you for having a look,

John
 
Rhodie,

Thank you for the thorough examination of my code. I'm going to give this a try and I'll let you know how it goes.

Thanks again,

John
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
No, I don't imagine it does. I think the problem is that you are referencing data directly on the sheet instead of lifting it into memory first and operating on the array of data in memory instead. you have a set array in
I agree doing it all in memory will be faster, but the number of cells involved is so small (tens of cells) that it's hard to believe that it will make a perceptible difference on the scale of human reaction time.
 
Upvote 0
I agree doing it all in memory will be faster, but the number of cells involved is so small (tens of cells) that it's hard to believe that it will make a perceptible difference on the scale of human reaction time.

It depends on the data sets in the worksheets and the formulas that are embedded in them too. We can't see what else has been written into the rest of the sheets.
 
Upvote 0
Sorry for my delay in getting back to you.

Rhodie72, I've placed you're code behind the button and I've still got the lag. The only formulas on this sheet are as follows.

Code:
=IF(SOV!$H5<>"",SOV!$H5/100-SOV!$F5/100,"")


Code:
=IFERROR((SOV!$C5*SOV!$G5),"")

and

Code:
=IF(C5<>"",C5*0.1,"")

This may be a dumb question, but when this is run does it somehow run other code I have in my workbook? There doesn't seem to be lag with anything else. I can provide a link to my workbook with sample data if that would help.

Thank you all for the great help so far.

John
 
Upvote 0
Sorry for my delay in getting back to you.

Rhodie72, I've placed you're code behind the button and I've still got the lag. The only formulas on this sheet are as follows.

Code:
=IF(SOV!$H5<>"",SOV!$H5/100-SOV!$F5/100,"")


Code:
=IFERROR((SOV!$C5*SOV!$G5),"")

and

Code:
=IF(C5<>"",C5*0.1,"")

This may be a dumb question, but when this is run does it somehow run other code I have in my workbook? There doesn't seem to be lag with anything else. I can provide a link to my workbook with sample data if that would help.

Thank you all for the great help so far.

John
As I pointed out in post #12, Rhodie's solution will make no perceptible difference in this case. It's possible that other code in the workbook is causing delay which is why I asked about other code in post #8.
 
Upvote 0

Forum statistics

Threads
1,216,031
Messages
6,128,424
Members
449,450
Latest member
gunars

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