Clearing of data works but if there's no data the code is deleting the headers

LeanneBG

Board Regular
Joined
Jun 20, 2016
Messages
157
Hi Experts - Good day! I have below code which is always the beginning of my process. I wan't to clear the table of any data for sheets Check and SRPT. The code is working as expected everytime there's data under the columns i'm trying to delete. However, if there's no data under the headers, the code below is deleting even the headers (which i don't want to happen..) Can you please help on this? For the formula on columns ZZ3:AAH - i wanted the code to start deleting on row 3 and not delete 1st and second rows. For the range A2:H, i want the code to start deleting from row 2 until the last row with data.. Hope someone can help. Thank you!



Code:
Sub Clear()
Application.ScreenUpdating = False
Dim Ws As Worksheet
Dim Last_Row As Long
Dim Formula_Row As Long


Sheets("Check").Select


ActiveSheet.AutoFilterMode = False


''This is solid for Advanced Filter:


On Error Resume Next
ActiveSheet.ShowAllData
Err.Clear


Last_Row = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:H" & Last_Row).Clear


Formula_Row = Range("AAE" & Rows.Count).End(xlUp).Row
Range("ZZ3:AAH" & Last_Row).Clear


ActiveSheet.[A1].Select


Sheets("SRPT").Select


ActiveSheet.AutoFilterMode = False


''This is solid for Advanced Filter:


On Error Resume Next
ActiveSheet.ShowAllData
Err.Clear


Last_Row = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:K" & Last_Row).Clear
Range("M2:S" & Last_Row).Clear
ActiveSheet.[A1].Select


Sheets("MENU").Select
MsgBox "All sheets are cleared, you may now proceed with checks"


Application.ScreenUpdating = True
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
This is untested:

Code:
Sub Clear()
Application.ScreenUpdating = False
Dim Ws As Worksheet
Dim Last_Row As Long
Dim Formula_Row As Long


Sheets("Check").Select


ActiveSheet.AutoFilterMode = False


''This is solid for Advanced Filter:


On Error Resume Next
ActiveSheet.ShowAllData
err.Clear


Last_Row = Range("A" & Rows.count).End(xlUp).Row
[COLOR=#0000cd]If Last_Row > 1 Then Range("A2:H" & Last_Row).Clear[/COLOR]


Formula_Row = Range("AAE" & Rows.count).End(xlUp).Row
[COLOR=#0000cd]If Last_Row > 2 Then Range("ZZ3:AAH" & Last_Row).Clear[/COLOR]


ActiveSheet.[A1].Select


Sheets("SRPT").Select


ActiveSheet.AutoFilterMode = False


''This is solid for Advanced Filter:


On Error Resume Next
ActiveSheet.ShowAllData
err.Clear


Last_Row = Range("A" & Rows.count).End(xlUp).Row
[COLOR=#0000cd]If Last_Row > 1 Then[/COLOR]
Range("A2:K" & Last_Row).Clear
Range("M2:S" & Last_Row).Clear
[COLOR=#0000cd]End If[/COLOR]
ActiveSheet.[A1].Select


Sheets("MENU").Select
MsgBox "All sheets are cleared, you may now proceed with checks"


Application.ScreenUpdating = True
End Sub
 
Upvote 0
Wait, this part:
Code:
Formula_Row = Range("AAE" & Rows.Count).End(xlUp).Row

Range("ZZ3:AAH" & Last_Row).Clear

The "Last_Row", shouldn't it be "Formula_Row"?

in that case, it should be :

If Formula_Row > 2 Then Range("ZZ3:AAH" & Formula_Row).Clear
 
Upvote 0
Wait, this part:
Code:
Formula_Row = Range("AAE" & Rows.Count).End(xlUp).Row

Range("ZZ3:AAH" & Last_Row).Clear

The "Last_Row", shouldn't it be "Formula_Row"?

in that case, it should be :

If Formula_Row > 2 Then Range("ZZ3:AAH" & Formula_Row).Clear

it works perfectly! Thank you sp much, Akuini! :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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