Vba to clear content but not header

IraAxa

Board Regular
Joined
Apr 23, 2013
Messages
115
Hi there,

i have a vba that clears content from specific cells, hence somehow it also deletes the header as well, how can i avoid so?

This is my code:

Dim LastRow As Long
Sheets("All").Select
LastRow = Range("J" & Rows.Count).End(xlUp).Row
Range("A2:J" & LastRow).ClearContents

Sheets("All").Select
LastRow = Range("AO" & Rows.Count).End(xlUp).Row
Range("AI2:AO" & LastRow).ClearContents
 
Well, that code will NOT clear the contents of row 1 so long as the lastRow Variable has a value greater than 1.
Even if Row1 and Row2 are merged.

There must be some other code that's doing it.

I recommend stepping through the code using F8 and watch Row1 at each step of the macro to see when it actually happens.
This is easier if you have 2 monitors, put the Excel on one monitor and the VBA window on the other.

If you don't have 2 monitors, you'll have to shrink each window so you can see both at the same time.
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This is all guessing games here now.

PM me if you want to email the file to me (with headers and data in rows below still in tact)
 
Upvote 0
Hi there,

i have a vba that clears content from specific cells, hence somehow it also deletes the header as well, how can i avoid so?

This is my code:

Dim LastRow As Long
Sheets("All").Select
LastRow = Range("J" & Rows.Count).End(xlUp).Row
If LastRow > 1 Then Range("A2:J" & LastRow).ClearContents

Sheets("All").Select
LastRow = Range("AO" & Rows.Count).End(xlUp).Row
If LastRow > 1 Then Range("AI2:AO" & LastRow).ClearContents

I am not entirely sure what is going on, but see if making the changes I highlighted in red above solves your problem.
 
Upvote 0
i think i got it:) i used the offset

Dim LastRow As Long
Sheets("All").Select
LastRow = Range("J" & Rows.Count).End(xlUp).Row
Range("A1:J" & LastRow).Offset (1). ClearContents
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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