Public Sub CopyData()
Dim wksSource As Worksheet
Dim wksTarget As Worksheet
Dim objTarget As Collection
Dim arrTarget As Variant
Dim lngCurrent As Long
Dim lngIndex As Long
Dim strKey As String
' Disable errors...
On Error Resume Next
' Sheets...
Set wksSource = Application.Workbooks("wksQ.xlsx").Worksheets("Budget")
Set wksTarget = Application.Workbooks("wksZ.xlsx").Worksheets("DATA_HW")
' Validate...
If Not wksSource Is Nothing And Not wksTarget Is Nothing Then
' Create...
Set objTarget = New Collection
' Target range...
'
' Das könnte dynamisiert werden, z.B. in Höhe = aktuelle
' Höhe + Höhe von Quelle, so dass ein maximaler Bereich
' ensteht
arrTarget = wksTarget.Range("A4:H999").Value
' Loop through target...
For lngIndex = LBound(arrTarget, 1) To UBound(arrTarget, 1)
If Len(arrTarget(lngIndex, 2)) > 0 And _
Len(arrTarget(lngIndex, 3)) > 0 Then
lngCurrent = lngCurrent + 1
objTarget.Add CStr(lngIndex), _
arrTarget(lngIndex, 2).Value & "-" & _
arrTarget(lngIndex, 3).Value
Else
Exit For
End If
Next
' Loop through source...
'
' Auch das könnte dynamisiert werden, so dass die Obergrenze
' per Code festgelegt wird
For lngIndex = 2 To 19
If Len(wksSource.Cells(lngIndex, 2).Value) > 0 And _
Len(wksSource.Cells(lngIndex, 3).Value) > 0 Then
strKey = ""
strKey = objTarget(wksSource.Cells(lngIndex, 2).Value & "-" & _
wksSource.Cells(lngIndex, 3).Value)
If Len(strKey) < 1 Then
lngCurrent = lngCurrent + 1
arrTarget(lngCurrent, 1) = lngCurrent
arrTarget(lngCurrent, 2) = wksSource.Cells(lngIndex, 2).Value
arrTarget(lngCurrent, 3) = wksSource.Cells(lngIndex, 3).Value
arrTarget(lngCurrent, 4) = wksSource.Cells(lngIndex, 4).Value
arrTarget(lngCurrent, 5) = wksSource.Cells(lngIndex, 5).Value
arrTarget(lngCurrent, 6) = wksSource.Cells(lngIndex, 6).Value
arrTarget(lngCurrent, 7) = wksSource.Cells(lngIndex, 7).Value
arrTarget(lngCurrent, 8) = wksSource.Cells(lngIndex, 8).Value
End If
End If
Next
' Write...
wksTarget.Range("A4:H999").Value = arrTarget
' Clear...
Set objTarget = Nothing
Else
MsgBox "Beide Mappen müssen offen sein.", vbOKOnly
End If
End Sub