VBA to clear specific cells in a single row

CastingDirector

New Member
Joined
Jun 10, 2014
Messages
46
So simple I can't get it.:eek: I need specific cells ("A1, A3, A5") to clear its contents after the (single) row is copied onto another sheet. Currently, I have it clearing the entire row (including a formula in A:3) when I only want these 3 cells to clear without removing the formula. Here Tiz:

Private Sub Worksheet_Change(ByVal Target As Range)


Application.ScreenUpdating = False

If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub

If Intersect(Target, Range("A:A")) = "No to Material" Then
Target.EntireRow.Copy Sheets("Non Avail").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
Target.EntireRow.ClearContents ' I would like to clear just 3 cells in this row, leaving the rest of the row as is

Can you fix this, please? Kind Regards
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Here's the problem. Let's say that you cleared cells A1, A2, and A3. Then you inserted that formula into A3 again. When you inset the formula, it will not be clear because you have a value in G11. It's just going to lookup the value again and your cell will not be clear again. So now you need to decide what you want to do about this before I can help. One solution I can think of is that you can write code that looks at cell A1 and if there is a value in A1 that is not blank, then it can insert that formula into A3.
Code:
IF Range("A1").value <> "" Then
Range("A3").value = "[COLOR=#333333]=IFERROR(VLOOKUP(G11,Resource!$A:$B,2,0),"""")"
[/COLOR]End if

Or you could just write a formula saying
=IF(A1<>"", IFERROR(VLOOKUP(G11,Resource!$A:$B,2,0),""),"")
Just don't clear A3 in your code.
 
Last edited:
Upvote 0
Thanks so much! I also thought this might work in VBA:
Target.EntireRow.Cells.SpecialCells(xlConstants).ClearContents
Both are quite viable.
Again my thanks and best regards!
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,798
Members
449,337
Latest member
BBV123

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