Macro to add up values in unhidden rows and clear if zwero

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I need a macro that when run looks at the the data in Range D25:BZ65) and add up each row, if the value for a row is more than 0 then just leave it but if the value in that row is 0 then clear cell B of that row.

please help if you can

Tony
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You didn't say what you want to happen if the value is less than zero so I'll assume your numeric content is such that the sum will always be 0 or greater.
Code:
Sub ClearB()
Dim R As Range, Rw As Range
Set R = Range("D25:BZ65")
For Each Rw In R.Rows
    If Application.Sum(Rw) = 0 Then
        If Rw.EntireRow.Hidden = False Then Rw.Cells(1, 1).Offset(0, -2).ClearContents
    End If
Next Rw
End Sub
 
Upvote 0
Thanks JoeMo,
yes completely right can never be less than zero,

thanks very much for your help


Tony
 
Upvote 0
I just realised I made a terrible mistake in my description ArrH!!!!

So sorry Joe Mo your code was excellent but I got it wrong,

where I said "Range D25:BZ65" I forgot to say but only the columns that are not hidden!

basically I need the formula to sum only Unhidden cells

can this be edited?
 
Upvote 0
I just realised I made a terrible mistake in my description ArrH!!!!

So sorry Joe Mo your code was excellent but I got it wrong,

where I said "Range D25:BZ65" I forgot to say but only the columns that are not hidden!

basically I need the formula to sum only Unhidden cells

can this be edited?
See if this does it:
Code:
Sub ClearB()
Dim R As Range, Rw As Range, c As Range, S As Double
Set R = Range("D25:BZ65")
For Each Rw In R.Rows
    S = 0
    For Each c In Rw.Cells
        If c.EntireColumn.Hidden = False Then S = S + c.Value
    Next c
    If S = 0 Then Rw.Cells(1, 1).Offset(0, -2).ClearContents
Next Rw
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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