Clearing Contents Efficiently

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I am attempting to clear contents of cells efficiently. Below is what I have but I am erroring out on the highlighted section. giving me a runtime error. i dont want to select, or activate any worksheets. trying to speed things up as best i can. just trying to clear contents of all data in Cells A starting on A13, E13:K13 and L14:X14. then everything in my compare data sheet.

VBA Code:
Sheets("Compare").Unprotect
With Sheets("Compare")
.Range("A13:A" & .Range("A" & rows.count).End(xlDown).row).ClearContents
End With

[B]With Sheets("Compare")
.Range("E13:K", .Range("E" & rows.count).End(xlDown).row).ClearContents
End With

With Sheets("Compare")
.Range("L14:X", .Range("L" & rows.count).End(xlDown).row).ClearContents
End With[/B]
        
With Sheets("Compare_DATA")
.Range("A:Z").ClearContents
End With
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Where do you get the runtime error? On what line

VBA Code:
.Range("E13:K", .Range("E" & rows.count).End(xlDown).row).ClearContents
should be
Code:
.Range("E13:K" & .Range("E" & rows.count).End(xlDown).row).ClearContents
and repeat same for the L14:X range, that should clear the errors, i hope
 
Upvote 0
You have an error in your lines of code. Rows.Count counts the total number of possible rows on a sheet.
So if you start there, you cannot do down, only up.

Change all these references:
..." & rows.count).End(xlDown).row).ClearContents
to
..." & rows.count).End(xlUp).row).ClearContents
 
Upvote 0
How about
VBA Code:
With Sheets("Compare")
   .Range("A13:A" & Rows.Count).ClearContents
   .Range("E13:K" & Rows.Count).ClearContents
   .Range("L14:X" & Rows.Count).ClearContents
End With
 
Upvote 0
How about
VBA Code:
With Sheets("Compare")
   .Range("A13:A" & Rows.Count).ClearContents
   .Range("E13:K" & Rows.Count).ClearContents
   .Range("L14:X" & Rows.Count).ClearContents
End With
appears to be working. Thanks! always appreciated
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

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