Первый совет: вместо Else: If
, просто используйте ElseIf
. Для уточнения:
If val1 Then
Action1
Else: If val2 Then
Action2
End If
End If
совпадает с написанием
If val1 Then
Action1
ElseIf val2 Then
Action2
End If
(:
имеет тот же эффект, что и начало новой строки, поэтому противоположность _
, которая "продолжается на следующей строке")
Теперь, когда вы заполняете ячейку, вы хотите изменить значение другой ячейки , которую вам нужно будет где-то определить - вот быстрый пример, который вам нужно будет изменить, чтобы он соответствовал причудам вашего конкретного листа:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub 'Only change 1 cell
If Target.Column <> 6 Then Exit Sub 'Column F only
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then Exit Sub 'Drop-Downs only
Dim OldValue As String, NewValue As String, TempValue As String
Dim rPersonnel As Range, rUnallocated As Range, rOnLeave As Range
Application.EnableEvents = False
Set rPersonel = Me.Cells(1, 6) 'Cell F1
Set rUnallocated = Me.Cells(2, 6) 'Cell F2
Set rOnLeave = Me.Cells(3, 6) 'Cell F3
If Not Intersect(Target, Union(rPersonnel, rOnLeave)) Is Nothing Then
NewValue = Target.Value
Application.Undo
OldValue = Target.Value
If OldValue = "" Then
Target.Value = NewValue
ElseIf InStr(1, OldValue, NewValue) = 0 Then
Target.Value = OldValue & vbLf & NewValue
Else
Target.Value = OldValue
End If
TempValue = Replace(Replace(rUnallocated.Value, NewValue, ""), vbLf & vbLf, vbLf) 'Remove from Unallocated and remove double-linebreaks
TempValue = Replace(Replace(Replace("|" & TempValue & "|", "|" & vbLf, ""), vbLf & "|", ""), "|", "") 'Remove start/end linebreak
rUnallocated.Value = TempValue
End If
If Not Intersect(Target, rPersonnel) Is Nothing Then 'You changed the "Personnel" list
TempValue = Replace(Replace(rOnLeave.Value, NewValue, ""), vbLf & vbLf, vbLf) 'Remove from On Leave and remove double-linebreaks
TempValue = Replace(Replace(Replace("|" & TempValue & "|", "|" & vbLf, ""), vbLf & "|", ""), "|", "") 'Remove start/end linebreak
rOnLeave.Value = TempValue
ElseIf Not Intersect(Target, rOnLeave) Is Nothing Then 'You changed the "OnLeave" list
TempValue = Replace(Replace(rPersonnel.Value, NewValue, ""), vbLf & vbLf, vbLf) 'Remove from Personnel and remove double-linebreaks
TempValue = Replace(Replace(Replace("|" & TempValue & "|", "|" & vbLf, ""), vbLf & "|", ""), "|", "") 'Remove start/end linebreak
rPersonnel.Value = TempValue
End If
Application.EnableEvents = True
End Sub