Good Morning,
I wrote the (not elegant) code below to make sure the correct data was entered or not entered in cells based on other cells. I used vba because we could only go so far with data validation and the Indirect function.
There are two problems. First, the code is slow, probably because of the many if/then's. Second, it only works for one row, and I need it for two. I tried copying and pasting, and changing the cell references, but it just got stuck in an endless loop.
I'm sure there must be a more concise and therefore faster way to accomplish this in code, as well as make it work for rows 39 AND 40.
Any help will be greatly appreciated.
Thank you,
-Jeff
Code:
Public Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim check As Boolean
Dim response
Dim somers As Boolean
Dim unit As Boolean
If Range("F39").Value = "" Then
Range("D39:e39").Value = ""
GoTo endsub
Else
If Range("f39") = "SMNH" Then
somers = True
Else: somers = False
End If
If Range("d39").Value = "" Then
unit = True
Else
unit = False
End If
If somers And unit = True Then
check = True
Else
check = False
End If
If check = True Then
response = MsgBox("Please Select A Unit", vbOKCancel, "Unit Not Selected")
If response = vbOK Then
Range("d39").Select
GoTo endsub
Else
Range("f39:g39").Value = ""
GoTo endsub
End If
Else
Range("f39").Select
End If
End If
If somers = False Then
If unit = False Then
Range("d39").Value = ""
End If
End If
endsub:
End Sub
I wrote the (not elegant) code below to make sure the correct data was entered or not entered in cells based on other cells. I used vba because we could only go so far with data validation and the Indirect function.
There are two problems. First, the code is slow, probably because of the many if/then's. Second, it only works for one row, and I need it for two. I tried copying and pasting, and changing the cell references, but it just got stuck in an endless loop.
I'm sure there must be a more concise and therefore faster way to accomplish this in code, as well as make it work for rows 39 AND 40.
Any help will be greatly appreciated.
Thank you,
-Jeff
Code:
Public Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim check As Boolean
Dim response
Dim somers As Boolean
Dim unit As Boolean
If Range("F39").Value = "" Then
Range("D39:e39").Value = ""
GoTo endsub
Else
If Range("f39") = "SMNH" Then
somers = True
Else: somers = False
End If
If Range("d39").Value = "" Then
unit = True
Else
unit = False
End If
If somers And unit = True Then
check = True
Else
check = False
End If
If check = True Then
response = MsgBox("Please Select A Unit", vbOKCancel, "Unit Not Selected")
If response = vbOK Then
Range("d39").Select
GoTo endsub
Else
Range("f39:g39").Value = ""
GoTo endsub
End If
Else
Range("f39").Select
End If
End If
If somers = False Then
If unit = False Then
Range("d39").Value = ""
End If
End If
endsub:
End Sub