Add formulae to rows that have data

dwg83

Board Regular
Joined
Nov 8, 2006
Messages
174
This code is running extremely slow. Is there a better way to do this so it won't be agonizingly slow.

Thanks!

Code:
    'adds formulae to rows that have data
    If Target.Column = 1 Then
        For Each i In Target.Rows
            If Cells(i.Row, 1) <> "" Then
                Cells(i.Row, 16) = "=IF(ISERROR(Q" & i.Row & "/R" & i.Row & "),"""",Q" & i.Row & "/R" & i.Row & ")"
                Cells(i.Row, 17) = "=IF(AND((B" & i.Row & "-C" & i.Row & ")>0,R" & i.Row & ">3),B" & i.Row & "-C" & i.Row & ",0)"
                Cells(i.Row, 18) = "=IF((B" & i.Row & "-M" & i.Row & ")>3,B" & i.Row & "-M" & i.Row & ",0)"
            Else
                Cells(i.Row, 16) = ""
                Cells(i.Row, 17) = ""
                Cells(i.Row, 18) = ""
            End If
        Next
    End If
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Is this part of a Worksheet_Change event procedure?
If so, you probably want to disable the events before making the updates so that the procedure doesn't keep calling itself as you change values.

Application.EnableEvents=False

Then when you are finished, turn events back on with

Application.EnableEvents=True
 
Upvote 0
Joe 4,

Thanks for that response, we were able to cut the time in half. i.e. For 8000 rows, we were able to reduce the time from 10 seconds to 5 seconds. If anyone else has recommendations on how to reduce the time it takes to fill in these formulae, please let me know!

Thanks again!

David
 
Upvote 0
Can you post your entire code, and explain how the data is being entered (what exactly is happening to trigger this code)?
 
Upvote 0
After stepping through the code for the worksheet change event, the only thing causing the long processing time is the code I provided. Your addition of the Enable events did its job to prevent going back through any of the worksheet change code again. I am wondering if I put the formulae in the first row and then filled in the rest of the rows as you would dragging down in excel would be faster. That is what I am trying next. Unfortunately, that will not be able to handle blanks in the middle of the range, it will just fill in those rows too.

Thanks!
David
 
Upvote 0
Another thing you can try is to put
Application.ScreenUpdating = False
at the top of your code and set it back at the end like this:
Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,207,421
Messages
6,078,436
Members
446,337
Latest member
nrijkers

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