Hi Boris
I'll have a go at explaining this line by line.
"Private Sub Worksheet_Change(ByVal Target As Range)"
This is a worksheet change event which means it runs every time any
cell/s on the sheet is/are changed. Target is a range object variable
which is the cell or cells that have been changed.
"If Target.Column < 3 And Target.Count = 1 Then"
This line checks that the cell changed is in columns A or B ie less than
column number 3 and that only one cell has been changed ie count = 1. If
either of these conditions is not met then nothing happens and the event
ends.
"Dim tRow As Long"
"tRow = Target.Row"
A variable called tRow is defined and set to be equal to the row of the
cell that has been changed i.e target.
"Cells(tRow, 3).Validation.Delete"
The existing validation in column C of the row in which the cell has
been change (tRow) is deleted
"If Cells(tRow, 1).Value <> Empty _
And Cells(tRow, 2).Value <> Empty Then"
This line checks that there are values in both column A and B of tRow.
If not nothing more is done. So if you did have validation in C5 and
then deleted the value in A5 then validation in C5 is removed and not
re-instated until A5 is populated again.
"If UCase(Cells(tRow, 2).Value) = "INTERNAL" Then"
Checks that the value is column B of tRow is Internal. The UCASE
statement changes the value into all upper case so that this check is
not case sensitive. If this is not Internal then nothing further is
done. So if you have validation in C5 and then change the value of B5
from External to Internal the validation will be removed.
With Cells(tRow, 3).Validation
.Add Type:=xlValidateList _
, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & Cells(tRow, 1).Value
.IgnoreBlank = True
.InCellDropdown = True
End With
Finally if all the requirements above are met i.e only one cell has been
changed, that cell is in either column A or B, both column A and B in
the row have values and the value in column B is Internal then standard
excel Data Validation is added to the cell in Column C. The validation
allows selection from a list and the list is a named range = to the
value in column A i.e. Formula1:="=" & Cells(tRow, 1).Value
End If
End If
End If
End Sub
And that's it.
I hope this has made it a little clearer. As with anything you are
testing I would suggest you save your work before trying this. If you
already have data on the sheet you could run the original macro I posted
to setup the validation. Then add this change event to keep the
validation dynamic so that it will respond to any changes that are made
to the sheet.
Regards
Rowan