I made the code idiot proof to work under any condition. this made th
code a little bit more complicate. If made made some asumptions I coul
of written the code with less steps.
I did two things.
1) I made the start date the 1st day of the current month. Otherwis
there would of been no October data. I'm using the forst day of th
month in the Tabelle1 sheet
2) I added code to put the dates in row 7 of sheet Tabelle1 at th
beginning of the macro. this is the only change I made. the dates wer
missing.
Now the code will look up each date and put it into the correct colum
no matter what order the dates are on sheet Tabelle2.
the results are putting 13 months of data into Tabelle1 (not 12) whic
is baed on the sample data you provided.
Option Explicit
Private Sub CommandButton1_Click()
Dim StartDate As Variant
Dim EndDate As Variant
Dim NewCol As Variant
Dim LastCol As Variant
Dim ColCount As Variant
Dim MyDate As Variant
Dim DestDates As Variant
Dim Data As Variant
Dim Dat As Variant
Dim StartCol As Integer
Dim MonthCount As Integer
Dim YearCount As Integer
StartDate = DateSerial(Year(Date), Month(Date), 1)
EndDate = DateAdd("yyyy", 1, Date)
With Sheets("Tabelle1")
Set DestDates = .Range("D7
7")
'enter the 12 months into row 7
StartCol = DestDates.Column
MonthCount = Month(StartDate)
YearCount = Year(StartDate)
For ColCount = StartCol To (StartCol + 12)
MyDate = DateSerial(YearCount, MonthCount, 1)
.Cells(7, ColCount).NumberFormat = "mmm-yy"
.Cells(7, ColCount) = MyDate
If MonthCount = 12 Then
MonthCount = 1
YearCount = YearCount + 1
Else
MonthCount = MonthCount + 1
End If
Next ColCount
End With
NewCol = 4 'column D
With Sheets("Tabelle2")
LastCol = .Cells(12, Columns.Count).End(xlToLeft).Column
For ColCount = 3 To LastCol
If IsDate(.Cells(12, ColCount)) Then
MyDate = .Cells(12, ColCount).Value
If MyDate >= StartDate And _
MyDate <= EndDate Then
Data = .Cells(13, ColCount)
With Sheets("Tabelle1")
For Each Dat In DestDates
If Month(MyDate) = Month(Dat) And _
Year(MyDate) = Year(Dat) Then
Dat.Offset(1, 0) = Data
Exit For
End If
Next Dat
End With
End If
End If
Next ColCount
End With
End Su