AW: Doppelte Namen in 2 Tabellenblättern anzeigen
14.08.2019 14:14:23
Pierre
Naja, war auch, wie gesagt, als Ansatz gedacht.
Du kannst den Teil von "If WorksheetFunction.CountIf" bis vor dem "For Each ws" weg lassen, dann wird nur auf dem jeweils anderen Tabellenblatt gesucht, nicht mehr innerhalb des gleichen.
Ich poste mal den angepassten Code.
So wie ich es verstehe, dürfte das genau das sein, was du benötigst.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Define your variables.
Dim ws As Worksheet, EvalRange As Range
'Set the range where you want to prevent duplicate entries.
Set EvalRange = Range("A1:B20")
'If the cell where value was entered is not in the defined range, if the value pasted is _
larger than a single cell,
'or if no value was entered in the cell, then exit the macro.
If Intersect(Target, EvalRange) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
If IsEmpty(Target) Then Exit Sub
'If the value entered already exists in the defined range on the current worksheet, throw _
an
'error message and undo the entry.
'If WorksheetFunction.CountIf(EvalRange, Target.Value) > 1 Then
'MsgBox Target.Value & " already exists on this sheet."
'Application.EnableEvents = False
'Application.Undo
'Application.EnableEvents = True
'End If
'Check the other worksheets in the workbook.
For Each ws In Worksheets
With ws
If .Name Target.Parent.Name Then
'If the value entered already exists in the defined range on the current _
worksheet, throw an
'error message and undo the entry.
If WorksheetFunction.CountIf(Sheets(.Name).Range("A1:B20"), Target.Value) > 0 _
Then
MsgBox Target.Value & " already exists on the sheet named " & .Name & ".", _
_
16, "No duplicates allowed in " & EvalRange.Address(0, 0) & "."
Application.EnableEvents = False
'Application.Undo
Application.EnableEvents = True
Exit For
End If
End If
End With
Next ws
End Sub
Den Teil, den ich anfangs meinte, habe ich auskommentiert. Außerdem musst du nur das "Application.Undo" weg nehmen, wenn deine (doppelte) Eingabe dennoch stehen bleiben soll.