I put an explanation on the lines of code, I called the file 'FileA' and 'FileB'. I have not renamed the sheets (I'm in GB). I considered that the entry of the month was to copy the file Sheet1 in cell O1 (to adapt according to your needs). Another important point, I considered that you open the FileB manually before launching the macro. I have to add that the macro pass red lines copied. This is certainly not the code more 'effective' but it has merit, I think, to be understandable, the goal being that you learn
Code:
Sub CopyDataAccordingMonth()
'
Dim MyMonth As Integer
Dim NbRows As Integer
Dim NbCol As Integer
Dim i As Integer
MyMonth = Sheets("sheet1").Range("o1") 'takes the value entered in the cell o1 of FileA
NbRows = Application.Subtotal(3, Sheets("sheet1").Range("b:b")) '
counts the number of line
For i = 2 To NbRows 'Loop trail that lines the column months
If Sheets("sheet1").Cells(i, 2) = MyMonth Then 'checks if the month of the line=choice
NbCol = Application.Subtotal(3, Sheets("sheet1").Range(i & ":" & i))
Sheets("sheet1").Range(Cells(i, 3), Cells(i, NbCol)).Copy 'Copy the line in question, only column C in the last columns.
Windows("FileB.xls").Activate 'pass on the FileB
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'Paste on first blank line
Windows("FileA.xls").Activate 'repass on FileA
Rows(i).Font.ColorIndex = 3 'applies the color red on the copied
End If
Next i
End Sub
Bookmarks