last row from c15 onwards

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello all

This code clear cells in my worksheet called "Result"

In row 15 from column "C:AA" there is a header in each cell

If I run this code it clears everything ok but if I run it again it clears the header then every time I run it it starts clearing the row above.

I think it has something to do with the "C16:AA" & LR code line

Can anyone please spot my mistake

Code:
Sub ClearContent()
Dim LR As Long
LR = Range("C" & Rows.Count).End(xlUp).Row
Sheets("Result").Range("D5:D13").ClearContents
Sheets("Result").Range("F5:F13").ClearContents
Sheets("Result").Range("C16:AA" & LR).ClearContents
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
That is because if there is no data in there, your header row (row 15) is your new last row, which will be cleared out.

So I would amend your code like this:
Code:
Sub ClearContent()
Dim LR As Long
LR = Range("C" & Rows.Count).End(xlUp).Row
Sheets("Result").Range("D5:D13").ClearContents
Sheets("Result").Range("F5:F13").ClearContents
[COLOR=#ff0000]If LR > 15 Then Sheets("Result").Range("C16:AA" & LR).ClearContents[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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