VBA to clear contents, not formulas of multiple rows based off a value in cell

efrana

New Member
Joined
Jul 6, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Was hoping for some help getting a code written that will work for me. I've found some different codes that work, but none specific. I'm basically new to VBA, so I'm really struggling piecing together a good understanding of these examples I'm finding.
Basically, by placing an "x" in any cell of column B, I'd like an automatic code that runs and clears contents(but not formulas) of that entire row, including the column B that was just entered? If an automated code is unattainable, I would be extremely content with a code I could attach to a button to manually run as needed?
I really appreciate any assistance, thanks!
I'm running Excel365. Currently, I have two codes attached to form control buttons that run various "auto sort" type functions in this spreadsheet, these are in the Module 2. I created those by recording my actions. Not sure if you needed to know this?
 
We cannot tell from an image what are formulas and what are not. We also cannot easily copy the data for testing. Please consider XL2BB for sample data.

Changing Columns("B") to Columns("K") in the previous code works for me when I enter something in K7, unless the "x" in F7 is the result of a formula. In what way does it not work for you?

The VBA code you last wrote for me is perfect and does what I need. When I place a value in the "K" cell, the entire row in cleared.

I was hoping to add another code into the mix though, one that would just clear out any cell content in cells ("E:J") only. I use these cells as a marker for where product is in a paint line. As product moves each day, I just place a value (marker) in the location where the product is (Jig, Blast, Prep, etc..). I would just like the "old" marker to clear out when I place the new location. Basically, only allowing one of the cells in each row to be "selected" at a time. The list gets rather long, so every "save" I can make on tabbing back and deleting as I move the products around is huge on time!

I hope I explained better and the example I attached will help.

Thanks as always!


Robot Paint Schedule.xlsm
ABCDEFGHIJK
1W/O ScheduleTypePaint CodePaint MixedJigWIPBay #BlastPrepReadyDone
2
3
48/31/23F4002Xx
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B1Cell Value="P"textNO
B1Cell Value="Q"textNO
E4:J1048576,E1:J1Cell Value="P"textNO
E4:J1048576,E1:J1Cell Value="Q"textNO
J4:J1048576,J1Cell Value="R"textNO
C4:C1048576,C1Cell Valuecontains "hold"textNO
H4:H1048576,H1Cell Value>0textNO
I4:I1048576,I1Cell Value>0textNO
J4:J1048576,J1Cell Value>0textNO
D1Other TypeIcon setNO
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this then to see if it is what you want.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim currVal As Variant
  
  ActiveSheet.Unprotect Password:="abc"
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Set Changed = Intersect(Target, Columns("K"))
  If Not Changed Is Nothing Then
    For Each c In Changed
      If Len(c.Value) > 0 Then c.EntireRow.SpecialCells(xlConstants).ClearContents
    Next c
  End If
  If Target.CountLarge = 1 And Not Intersect(Target, Columns("E:J")) Is Nothing And Target.Row + 1 Then
    currVal = Target.Value
    Intersect(Target.EntireRow, Columns("E:J")).ClearContents
    Target.Value = currVal
  End If
  ActiveSheet.Protect Password:="abc"
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Private Sub Worksheet_Change(ByVal Target As Range) Dim Changed As Range, c As Range Dim currVal As Variant ActiveSheet.Unprotect Password:="abc" Application.ScreenUpdating = False Application.EnableEvents = False Set Changed = Intersect(Target, Columns("K")) If Not Changed Is Nothing Then For Each c In Changed If Len(c.Value) > 0 Then c.EntireRow.SpecialCells(xlConstants).ClearContents Next c End If If Target.CountLarge = 1 And Not Intersect(Target, Columns("E:J")) Is Nothing And Target.Row + 1 Then currVal = Target.Value Intersect(Target.EntireRow, Columns("E:J")).ClearContents Target.Value = currVal End If ActiveSheet.Protect Password:="abc" Application.EnableEvents = True Application.ScreenUpdating = True End Sub
Again, you do good work! This was exactly what I was looking for, thank you so much!
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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