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
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,575
Office Version
  1. 365
Platform
  1. Windows
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
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,575
Office Version
  1. 365
Platform
  1. Windows
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
 

LeanneBG

Board Regular
Joined
Jun 20, 2016
Messages
157
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! :)
 

Watch MrExcel Video

Forum statistics

Threads
1,108,612
Messages
5,523,891
Members
409,542
Latest member
Shezz01

This Week's Hot Topics

Top