Allowing ClearContents macro to run with error

Darren_workforce

Board Regular
Joined
Oct 13, 2022
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a large amount of code but the only part I am having issues with is below. Each day's data varies on length so if we have 15 agents versus 30, the number of rows will change. But there will never be 99 agents so I just used that to cover any particular day. On the last line, though, there is always going to be an average line that averages out every column from D:Q. When the cells are being cleared, if all values for a certain metric are zero, the average cell populates a #DIV/0 error. This seems to be what's causing the code to get hung up.

When I go into debug and then delete the cells with the DIV/0 error and then Step Out to run the rest of the macro, it works. So it has to be the error, right?

Is there a way to add a line so that the ClearContents request ignores the errors?


VBA Code:
Dim cell, rng As Range
Set rng = Range("D2:Q99")

For Each cell In rng
    If cell.Value = "0" Then
    cell.ClearContents
    Else
    End If
Next cell
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try:
VBA Code:
Dim cell as range
Dim x as Long

x = Range("Q" and Rows.Count).End(xlUp).Row

For each cell in Range("D2:Q" & x)
     If cell.Value = 0 Then cell.ClearContents
Next cell

If that doesn't work, you could try wrapping an On Error Resume Next outside of the loop, but it may cause other issues for valid errors, however:
VBA Code:
Dim cell as range
Dim x as Long

x = Range("Q" and Rows.Count).End(xlUp).Row

On Error Resume Next
For each cell in Range("D2:Q" & x)
     If cell.Value = 0 Then cell.ClearContents
Next cell
On Error Goto 0

Also, are the cells in D2:Q99 filled with formulae? If so, your code is testing for the string "0" rather than the number 0 - reason I've omitted the speech marks from my suggestion
 
Upvote 0
It still generates the Debug popup and the following line is highlighted (both of your suggestions):

VBA Code:
x = Range("Q" And Rows.Count).End(xlUp).Row
 
Upvote 0
Haven't done VBA in a while, try
VBA Code:
x = Range("Q" & Rows.Count).End(xlUp).Row
Or
VBA Code:
x = cells(rows.count, "Q").End(xlup).Row
 
Upvote 0
Solution

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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