VBA to clear all content below row 1

JDeMaro22
StarLounger
Posts: 94
Joined: 16 Oct 2021, 16:22

VBA to clear all content below row 1

Post by JDeMaro22 »

Hello,

For some reason the VBA i'm using to clear all contents below row 1 is now just closing the entire workbook when I press the form control button I have assigned to it. Is there a different code I could use to perform the same task?

Sub ClearData()
With Sheets("Paste Data Here!")
.Rows(2 & ":" & .Rows.Count).ClearContents
End With
End Sub

Thank you,

Joshua

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to clear all content below row 1

Post by HansV »

The code looks OK, and it works as intended, without error, when I test it.
Does the problem persist after quitting and restarting Excel?

A slightly different way of writing it:

Code: Select all

Sub ClearData()
    With Sheets("Paste Data Here!")
        .Range("A2:A" & .Rows.Count).EntireRow.ClearContents
    End With
End Sub
or

Code: Select all

Sub ClearData()
    With Sheets("Paste Data Here!").Cells
        .Resize(.Rows.Count - 1).Offset(1).ClearContents
    End With
End Sub
Best wishes,
Hans

JDeMaro22
StarLounger
Posts: 94
Joined: 16 Oct 2021, 16:22

Re: VBA to clear all content below row 1

Post by JDeMaro22 »

It was very odd, yeah it would shut down my whole workbook when I used. I've tried your code and it seems to be working properly now.

Thanks again Hans

snb
4StarLounger
Posts: 582
Joined: 14 Nov 2012, 16:06

Re: VBA to clear all content below row 1

Post by snb »

Code: Select all

Sub M_snb()
 Sheets("Paste Data Here!").usedrange.offset(1).clearcontents
End Sub