Excel 2013 VBA

darrenbutt

New Member
Joined
May 31, 2018
Messages
7
I am looking for some assistance with VBA as I am extremely new to this...

I have a drop down menu in cells D8:D28 that have 5 options (Elec, Mech, Op, C/Over and Project). I want to stop people selecting either Elec or Mech unless there is a numeric value in cell N8:N28.

Please if anyone can help I would be greatly appreciative.

Regards,

Darren
 
Hi Rick,

Can this code be expanded - If someone is clever enough you can currently enter a numeric value into N8:N28 to allow you to select Elec or Mech from the drop down but you can then go and delete the numeric value. Can the code stop someone from deleting the numeric value if either Elec or Mech is selected from the drop down menu is cells D8:D28.
Give the following a try... what it will do is clear the corresponding cell in Column D if the value in Column N is cleared (I highlighted in blue the change I made that makes this happen).
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Value As Variant
  If Target.Count <> 1 Then Exit Sub
  If Not Intersect(Target, Range("D8:D28")) Is Nothing Then
    Value = Intersect(Target.EntireRow, Columns("N")).Value
    If Value Like "*[!0-9.]*" Or Value Like "*.*.*" Or Len(Value) = 0 Or Value = "." Then
      If Target.Value = "Elec" Or Target.Value = "Mech" Then
        MsgBox "Sorry but you cannot select ""Elec"" or ""Mech"" unless there is a number in cell N" & Target.Row & "."
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
      End If
    End If
  [B][COLOR="#0000FF"]ElseIf Not Intersect(Target, Range("N8:N28")) Is Nothing Then
    If Target.Value = "" Then Target.Offset(, -10).Value = ""[/COLOR][/B]
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Rick,

Worked a treat! much appreciated. Is there an easy way to se the numeric value so it must be between 900000000 and 999999999 ? I did attempt to myself but it didn't work out too well.

Cheers!
 
Upvote 0
Worked a treat! much appreciated. Is there an easy way to se the numeric value so it must be between 900000000 and 999999999 ? I did attempt to myself but it didn't work out too well.
Try changing this line of code...
Code:
[table="width: 500"]
[tr]
	[td]If Value Like "*[!0-9.]*" Or Value Like "*.*.*" Or Len(Value) = 0 Or Value = "." Then[/td]
[/tr]
[/table]
to this...
Code:
[table="width: 500"]
[tr]
	[td]If Value Like "*[!0-9.]*" Or Value Like "*.*.*" Or Len(Value) = 0 Or Value = "." [B][COLOR="#0000FF"]Or Value Like "[0-8]*"[/COLOR][/B] Then[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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