Clear Row Contents (Excluding Column A & N)

tomexcel1

New Member
Joined
Feb 22, 2018
Messages
47
Hi All

I have the following code that clears the contents of a row based on the text in cell G. However I need the code to clear contents of the row but exclude anything in Column A and N.

Code:
Sub ClearIncorrectRows()Dim lRw As Long, n As Long
lRw = Range("G" & Rows.Count).End(xlUp).Row
For n = 1 To lRw
     If Range("G" & n).Value = "DELETE ROW" Then Range("G" & n).EntireRow.ClearContents
Next n
End Sub

Any help would be most appreciated.

Thanks
Tom
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try something like this:-
Code:
Sub ClearIncorrectRows()
Dim lRw As Long, n As Long, Temp1 As String, Temp2 As String
lRw = Range("G" & Rows.Count).End(xlUp).Row
For n = 1 To lRw
     If Range("G" & n).Value = "DELETE ROW" Then
        Temp1 = Range("A" & n): Temp2 = Range("N" & n)
            Range("G" & n).EntireRow.ClearContents
        Range("A" & n) = Temp1: Range("N" & n) = Temp2
    End If
Next n
End Sub
 
Upvote 0
Hi MickG!

Thanks for your help, it works great for column A but still seams to delete whats in column N. Could it be something to do with the fact column N as a formula in it?
 
Upvote 0
How about
Code:
Sub clearrws()
   Range("A:A,N:N").EntireColumn.Hidden = True
   With Range("G1", Range("G" & Rows.Count).End(xlUp))
      .Replace "delete row", "=XXX", xlWhole, , False, , False, False
      .SpecialCells(xlFormulas, xlErrors).EntireRow.SpecialCells(xlVisible).ClearContents
   End With
   Range("A:A,N:N").EntireColumn.Hidden = False
End Sub
 
Upvote 0
Try changing code as shown in red :-
Code:
 Temp1 = Range("A" & n): Temp2 = Range("N" & n[B][COLOR=#FF0000]).Formula[/COLOR][/B]
 
Upvote 0
Assuming you will run this when you know there is at least one "Delete Row" cell, here is another macro that should work...
Code:
[table="width: 500"]
[tr]
	[td]Sub ClearIncorrectRows()
  Columns("G").Replace "Delete Row", "#N/A", xlWhole, , False, , False, False
  Intersect(Range("B:M,O:" & Split(Columns(Columns.Count).Address, "$")(2)), Columns("G").SpecialCells(xlConstants, xlErrors).EntireRow).ClearContents
End Sub[/td]
[/tr]
[/table]
If you cannot be sure that there will be at least one "Delete Row" cell, then use this instead...
Code:
[table="width: 500"]
[tr]
	[td]Sub ClearIncorrectRows()
  Columns("G").Replace "Delete Row", "#N/A", xlWhole, , False, , False, False
  On Error Resume Next
  Intersect(Range("B:M,O:" & Split(Columns(Columns.Count).Address, "$")(2)), Columns("G").SpecialCells(xlConstants, xlErrors).EntireRow).ClearContents
  On Error GoTo 0
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
MICK G.

This is great.

Question - Are you storing the data in Range A : N in Temp 1 & Temp 2 and then clearing the contents if the value in Column G is "DELETE ROW" then reapplying the stored data.
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,904
Members
449,477
Latest member
panjongshing

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