AW: Felder in 2 Tabellen abgleichen
08.06.2006 11:11:57
fcs
Hallo Helmar,
ich hab die Makros jetzt etwas angepasst, so dass weitere Spalten einfach ergänzt werden können. Für jede weitere Spalte muß du nur jeweils eine weitere Case-Anweisung einfügen.
Makros für Blatt1:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim wks1 As Worksheet, wks2 As Worksheet, Finden As Range
Dim SpS1 As Long, SpS2 As Long, Sp As Long
Set wks1 = ThisWorkbook.Sheets("Tab1")
Set wks2 = ThisWorkbook.Sheets("Tab2")
SpS1 = 1 'Spalte mit Schlüsselsfeldern in Blatt 1
SpS2 = 1 'Spalte mit Schlüsselsfeldern in Blatt 2
Select Case Target.Column
Case 2 'Spalte in Blatt 1
Sp = 2 'ensprechende Spalte in Blatt 2
Case 3
Sp = 4
Case Else
' Do nothing
Exit Sub
End Select
Set Finden = wks2.Columns.Find(What:=wks1.Cells(Target.Row, SpS1), LookIn:=xlValues, Lookat:=xlWhole)
If Finden Is Nothing Then
If MsgBox("Schlüssel in Tabelle " & wks2.Name & " nicht vorhanden." & vbLf & vbLf _
& "Zeile in" & wks2.Name & " einfügen?", vbYesNo, "Tabellenabgleich") = vbYes Then
Call ZeilenDatenuebertragen1(wks1, wks2, Target.Row, SpS2)
End If
Else
If wks2.Cells(Finden.Row, Sp).Value <> Target.Value Then
wks2.Cells(Finden.Row, Sp).Value = Target.Value
End If
End If
End Sub
Sub ZeilenDatenuebertragen1(Blatt1 As Worksheet, Blatt2 As Worksheet, Zeile1 As Long, SpS2 As Long)
' Fügt Daten einer Zeile aus Blatt1 in Blatt2 am Ende ein
Dim Zeile2 As Long
Zeile2 = Blatt2.Cells(65000, SpS2).End(xlUp).Row + 1 'nächste leere Zeile in Blatt 2
Blatt2.Cells(Zeile2, 1).Value = Blatt1.Cells(Zeile1, 1).Value
Blatt2.Cells(Zeile2, 2).Value = Blatt1.Cells(Zeile1, 2).Value
Blatt2.Cells(Zeile2, 4).Value = Blatt1.Cells(Zeile1, 3).Value
End Sub
Makros für Blatt2:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim wks1 As Worksheet, wks2 As Worksheet, Finden As Range
Dim SpS1 As Long, SpS2 As Long, Sp As Long
Set wks1 = ThisWorkbook.Sheets("Tab1")
Set wks2 = ThisWorkbook.Sheets("Tab2")
SpS1 = 1 'Spalte mit Schlüsselsfeldern in Blatt 1
SpS2 = 1 'Spalte mit Schlüsselsfeldern in Blatt 2
Select Case Target.Column
Case 2 'Spalte in Blatt 2
Sp = 2 'ensprechende Spalte in Blatt 1
Case 4
Sp = 3
Case Else
' Do nothing
Exit Sub
End Select
Set Finden = wks1.Columns.Find(What:=wks2.Cells(Target.Row, SpS2), LookIn:=xlValues, Lookat:=xlWhole)
If Finden Is Nothing Then
If MsgBox("Schlüssel in Tabelle " & wks1.Name & " nicht vorhanden." & vbLf & vbLf _
& "Zeile in" & wks1.Name & " einfügen?", vbYesNo, "Tabellenabgleich") = vbYes Then
Call ZeilenDatenuebertragen2(wks1, wks2, Target.Row, SpS1)
End If
Else
If wks1.Cells(Finden.Row, Sp).Value <> Target.Value Then
wks1.Cells(Finden.Row, Sp).Value = Target.Value
End If
End If
End Sub
Sub ZeilenDatenuebertragen2(Blatt1 As Worksheet, Blatt2 As Worksheet, Zeile2 As Long, SpS1 As Long)
' Fügt Daten einer Zeile aus Blatt2 in Blatt1 am Ende ein
Dim Zeile1 As Long
Zeile1 = Blatt1.Cells(65000, SpS1).End(xlUp).Row + 1 'nächste leere Zeile in Blatt 2
Blatt1.Cells(Zeile1, 1).Value = Blatt2.Cells(Zeile2, 1).Value
Blatt1.Cells(Zeile1, 2).Value = Blatt2.Cells(Zeile2, 2).Value
Blatt1.Cells(Zeile1, 3).Value = Blatt2.Cells(Zeile2, 4).Value
End Sub