Need code that clear specific contents

wroze27

New Member
Joined
Apr 23, 2021
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
For a project I'm doing at work I need to be able to clear certain cells in the last line of a table. I want to clear the cells in columns B,C,J,K,L,M,N,O,P while leaving the formulas in the other in place.
Currently I have this which only clears the whole row:
Sub Clear()

Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Rows(lr).ClearContents

End Sub

Thanks for any advice
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,633
Office Version
  1. 365
Platform
  1. Windows
Welcome to the Board!

Here is one way:
VBA Code:
Sub MyClear()

    Dim lr As Long
    Dim rng1 As Range
    Dim rng2 As Range
    
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
    Set rng1 = Intersect(Rows(lr), Range("B:C"))
    Set rng2 = Intersect(Rows(lr), Range("J:P"))
    rng1.ClearContents
    rng2.ClearContents

End Sub
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,133,243
Messages
5,657,575
Members
418,401
Latest member
B_A_M155

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
Top