VBA code to delete contents in a range then prevent further input to the range until correct criteria is met.

Declamatory

Active Member
Joined
Nov 6, 2014
Messages
319
I have a dropdown in cell D8 which when selected populates a number of cells but nothing in column F. The dropdown has five options.

Could anyone provide me with VBA coding that deletes content in a range in column F and then prevents people from inputting to that range unless the correct option in the dropdown in cell D8 has been selected.

Only one of the dropdown options should allow input to the range in column F. Any other option in cell D8 would delete the content in the range in column F and prevent further input to the range until the correct dropdown option has been selected.

Thanks in anticipation.

Declamatory
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have a dropdown in cell D8 which when selected populates a number of cells but nothing in column F. The dropdown has five options.

Could anyone provide me with VBA coding that deletes content in a range in column F and then prevents people from inputting to that range unless the correct option in the dropdown in cell D8 has been selected.

Only one of the dropdown options should allow input to the range in column F. Any other option in cell D8 would delete the content in the range in column F and prevent further input to the range until the correct dropdown option has been selected.

Thanks in anticipation.

Declamatory

Figured it out in the end Phew!:) Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ReEnableEvents
Application.EnableEvents = False
If Target.Address = Range("D8").Address Then
Select Case UCase(Target.Value)
Case ""
Application.Undo
GoTo ReEnableEvents
Case UCase("Option 1")
Range("F10:F48").ClearContents
Case UCase("Option 2")
Range("F10:F48").ClearContents
Case UCase("Option 3")
Range("F10:F48").ClearContents
Case UCase("Option 4")
Range("F10:F48").ClearContents
End Select
End If
ReEnableEvents:
Application.EnableEvents = True

If Intersect(Target, Range("F10:F48")) Is Nothing Then Exit Sub
On Error GoTo ExitPoint
Application.EnableEvents = False
If Range("D8").Value <> "Option 5" Then
Application.Undo
MsgBox "Cell D8 must be Option 5 in order to input to this cell", vbCritical, "Error"
End If
ExitPoint:
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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