Macro to find total and clear contents all rows below total

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have written code to find "total" in column A and to clear the data below total in Cols A to O

The code only clears one row of data

Your assistance in this regard is most appreciated


Code:
 Sub Clear_Data_Below_Total()
finalrow = Range("A65536").End(xlUp).Row
For I = finalrow To 1 Step -1

Cells(finalrow, I).Resize(10, 15).ClearContents
Next I

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How does that code find "total" row
It appears to find the last row only
 
Upvote 0
Hi Michael

Total is on Col A after the last data

I don't know how to incorporate this in my code

It would be appreciated if you could help
 
Upvote 0
Hi howard

With this formula in A20 =SUM(A1:A19) (nothing below A20 in column A).

And data in cells B2:O40.

Run the code and the data from B21:O30 is deleted.

Does that do it?

Howard (same name, different Howard)

Code:
Option Explicit

Sub Clear_Data_Below_Total()
Dim finalrow As Long, i As Long

finalrow = Range("A65536").End(xlUp).Row + 1
For i = finalrow To 1 Step -1

Cells(finalrow, i).Resize(10, 15).ClearContents
Next i

End Sub
 
Upvote 0
Thanks for the help much appreciated

When running the code, the data below Total is not cleared


Kindly amend your code


See sample data below



Excel 2012
ABCDEFGHI
1Br11
2Br22
3Br32
4Br42
5Br52
6
7
8Total9
9
10
11
12111111111
13111111111
14111111111
15
16
Sheet1
 
Upvote 0
Try this

Code:
Sub Clear_Data_Below_Total()
    finalRow = Range("A65536").End(xlUp).Row
    totalRow = Range("A1:A" & finalRow).Find("Total", LookIn:=xlValues).Row
    Range("A" & totalRow + 1 & ":O" & finalRow).ClearContents
End Sub

Assumes your last row with data is in col A
 
Upvote 0
Maybe this, which is what you started out with I think.

I may not understand your needs.


This

finalrow = Range("A65536").End(xlUp).Row + 1

to

finalrow = Range("A65536").End(xlUp).Row

Howard

Edit, looks like you have data in column A below the TOTAL?

Howard
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,222
Members
448,877
Latest member
gb24

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