Macro to Delete values from Row number 3

ragav_in

Board Regular
Joined
Feb 13, 2006
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Dear All,

I have a specific request to delete the text from the Row 3 onwards, as the top 2 rows are considered to be template text and needs to be retained. I have written the below code which performs the deletion as expected. However the same macro when run on the sheet when it has only the first 2 rows of data, deletes the 2nd row which it should not do. Can someone help me identify on what am I doing wrong here. The expectation here is that the macro should always delete text from Row 3 onwards irrespective if there is text or not in the sheet. First 2 columns should not be deleted.
List of Reports
Column 1Column 2Column 3Column 4Column 5
Text 1Text 2Text 3Text 4Text 5
Text 6Text 7Text 8Text 9Text 10
VBA Code:
Sub ClearFilledCells()
Dim Lr, Lc As Long, sht As Worksheet, firstcell As Range
Set sht = ThisWorkbook.ActiveSheet
Set firstcell = Range("A3")
If ActiveSheet.Name <> "MySheet" Then
    Lr = Range("A3").SpecialCells(xlCellTypeLastCell).row
    Lc = Range("A3").SpecialCells(xlCellTypeLastCell).Column
    sht.Range(firstcell, sht.Cells(Lr, Lc)).Select
    Selection.ClearContents
    Selection.ClearFormats
    Range("A1").Select
Else: MsgBox "Data from MySheet cannot be deleted"
End If
End Sub

I thank everyone who takes time and effort to look into this and provides me some suggestion.

Thanks
ragav_in
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Just an add IF statement to check to see if the last row is 3 or greater, i.e.
VBA Code:
Sub ClearFilledCells()
Dim Lr, Lc As Long, sht As Worksheet, firstcell As Range
Set sht = ThisWorkbook.ActiveSheet
Set firstcell = Range("A3")
If ActiveSheet.Name <> "MySheet" Then
    Lr = Range("A3").SpecialCells(xlCellTypeLastCell).row
    Lc = Range("A3").SpecialCells(xlCellTypeLastCell).Column
    If Lr >= 3 Then
        With sht.Range(firstcell, sht.Cells(Lr, Lc))
            .ClearContents
            .ClearFormats
        End With
        Range("A1").Select
    End If
Else: MsgBox "Data from MySheet cannot be deleted"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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