Can I Link VBA clear contents macro to Cell Criteria from Dropdown selection

Dazzybeeguy

Board Regular
Joined
Jan 6, 2022
Messages
72
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi

I want to allow the user to change a cell contents to a given Criteria

I wanted to add Validation droop down where they select Del in cell B9

I want to create VBA that on that action would clear the cell contents of a range C9:BH9

Is that possible, I cant seem to be able to assign a macro on that basis

Sub clear9()

If Range("b9").Value = "Del" Then
Range("c9:bh9").ClearContents
End If

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi

I want to allow the user to change a cell contents to a given Criteria

I wanted to add Validation droop down where they select Del in cell B9

I want to create VBA that on that action would clear the cell contents of a range C9:BH9

Is that possible, I cant seem to be able to assign a macro on that basis

Sub clear9()

If Range("b9").Value = "Del" Then
Range("c9:bh9").ClearContents
End If

End Sub
If I insert that in a module and run it then it clears the content, but when I change the dropdown in cell B9 to Del it doesnt do anything
 
Upvote 0
Th code belongs in the worksheet module.
Right click on the sheet tab and select "View Code"

Paste this code there.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address = "$B$9" Then
If Target = "Del" Then
Range("C9:BH9").ClearContents
End If
End If

End Sub

If you want to have it dynamic so it works for every row in column B

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 2 Then
If Target = "Del" Then
Range("C" & Target.Row & ":BH" & Target.Row).ClearContents
End If
End If

End Sub
 
Upvote 0
Solution
Th code belongs in the worksheet module.
Right click on the sheet tab and select "View Code"

Paste this code there.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address = "$B$9" Then
If Target = "Del" Then
Range("C9:BH9").ClearContents
End If
End If

End Sub

If you want to have it dynamic so it works for every row in column B

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 2 Then
If Target = "Del" Then
Range("C" & Target.Row & ":BH" & Target.Row).ClearContents
End If
End If

End Sub
Thanks, the Dynamic one works, is there a VBA way to reset column B on Exit to turn any that had been changed to "Del" back to "N" ???
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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