Datenaustausch zwischen 2 Excel Dateien

  • Excel

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Petersilie.

    Datenaustausch zwischen 2 Excel Dateien

    Hallo zusammen,

    ich habe folgendes Problem.

    Ich habe zwei Excel Dateien A und B.

    Sei A = Masterfile und B = Excel File, die jeden Tag geupdatet wird

    A und B haben die selbe Tabelle, von Spalte ID bis hin zu Erstellungsdatum.

    Ich möchte nun in A ein VBA Code schreiben, welcher bei Ausführung in B, in die Spalte ID geht, nacheinander schaut, ob die aktuelle ID in A enthalten ist, falls ja tue nichts, falls nein, kopiere die gesamte Zeile von B und füge sie in A ein.
    Das heißt dass alle IDs die nicht in A enthalten sind (per Definition also neue IDs) aus B in A geschrieben werden sollen.

    Ist wohl kein Hexenwerk, mit einer Schleife und einem IF lösbar, denke ich. Aber wie schaue ich denn ob die ID aus B bereits in A enthalten ist? Etwa mit der .Find Methode?

    Besten Dank an alle :thumbsup:
    Hallo,

    hier etwas ohne .Find oder .Match, man könnte bei dem Code aber noch .Match oder .Find mit reinnehmen.
    In dem Code unten musst du Cell, Range und Worksheet Referenzen auf dich anpassen.
    (btw. Application.Match ist wesentlich schneller als Range.Find sofern du nur in einer Spalte suchst)
    (Kommentare sind auf Englisch, kleine Angewohnheit)

    Visual Basic-Quellcode

    1. Option Explicit
    2. Private MasterFile As Worksheet
    3. Private TempFile As Worksheet
    4. Public Sub UpdateMaster()
    5. Dim MasterIDs As Object
    6. Dim NewIDs As Object
    7. 'Set Sheets
    8. Set MasterFile = ThisWorkbook.Sheets("Masterdatei")
    9. Set TempFile = ThisWorkbook.Sheets("Temp")
    10. 'Create Dictionarys
    11. Set MasterIDs = ExistingIDs
    12. Set NewIDs = AddNewIDs(MasterIDs)
    13. 'Exit if there arent any
    14. If NewIDs Is Nothing Then Exit Sub
    15. Application.ScreenUpdating = False
    16. 'Worksheet sometimes wont update directly
    17. 'if that happens just click in the sheet
    18. AddNewDatasets NewIDs
    19. Application.ScreenUpdating = True
    20. Set MasterFile = Nothing
    21. Set TempFile = Nothing
    22. End Sub
    23. 'Get all the existing IDs
    24. Private Function ExistingIDs() As Object
    25. Dim array_, varItem As Variant
    26. Dim dictionary_ As Object
    27. Dim rng As Range
    28. Dim lRow As Long
    29. Set dictionary_ = CreateObject("Scripting.Dictionary")
    30. With MasterFile
    31. lRow = .Cells(.Rows.Count, 3).End(xlUp).Row
    32. Set rng = .Range(.Cells(2, 3), .Cells(lRow, 3))
    33. array_ = rng
    34. For Each varItem In array_
    35. If varItem <> "" And Not dictionary_.Exists(varItem) Then
    36. dictionary_.Add varItem, Nothing
    37. End If
    38. Next varItem
    39. End With
    40. Set ExistingIDs = dictionary_
    41. Set dictionary_ = Nothing
    42. Set rng = Nothing
    43. Erase array_
    44. End Function
    45. 'Get all the New IDs and safe the range as Item
    46. Private Function AddNewIDs(ByRef ExistingData As Object) As Object
    47. Dim tmpRange As Range
    48. Dim rng, c As Range
    49. Dim lRow As Long
    50. With TempFile
    51. lRow = .Cells(.Rows.Count, 3).End(xlUp).Row
    52. Set rng = .Range(.Cells(2, 3), .Cells(lRow, 3))
    53. For Each c In rng
    54. If c.Value <> "" And Not ExistingData.Exists(c.Value) Then
    55. Set tmpRange = .Range(.Cells(c.Row, 1), .Cells(c.Row, 10))
    56. ExistingData.Add c.Value, tmpRange
    57. Set tmpRange = Nothing
    58. End If
    59. Next c
    60. End With
    61. Set AddNewIDs = ExistingData
    62. Set ExistingData = Nothing
    63. Set c = Nothing
    64. Set rng = Nothing
    65. End Function
    66. 'Add all the new Datasets to the Mastersheet
    67. Private Sub AddNewDatasets(ByRef DataToAdd As Object)
    68. Dim lRow As Long
    69. Dim varItem As Variant
    70. Dim tmpRange As Range
    71. lRow = MasterFile.Cells(MasterFile.Rows.Count, 3).End(xlUp).Row + 1
    72. For Each varItem In DataToAdd.Keys
    73. If Not DataToAdd(varItem) Is Nothing Then
    74. Set tmpRange = DataToAdd(varItem)
    75. MasterFile.Range("A" & lRow).Resize(tmpRange.Rows.Count, tmpRange.Columns.Count) = tmpRange.Value
    76. Set tmpRange = Nothing
    77. lRow = lRow + 1
    78. End If
    79. Next varItem
    80. Set DataToAdd = Nothing
    81. End Sub