Search code examples
excelvba

Copy a series of data matrices 14 columns and 33 rows to a new sheet


I need to copy a series of data matrices 14 columns and 33 rows to a new sheet.

From the first copy all the values including the labels, from the others only the numerical values.

Sub title_full()
Dim sheet1 As Worksheet
Dim last_row As Long

'MsgBox (last_row)
'Sheets("Gennaio").Range("A4:N35").SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("A").Range("A4")
Sheets("Gennaio").Range("A5:N36").Copy Destination:=Sheets("A").Range("A4")
last_row = Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Febbraio").Range("A6:N33").Copy Destination:=Sheets("A").Range("A" & (last_row + 1))
Sheets("Marzo").Range("A6:N33").Copy Destination:=Sheets("A").Range("A" & (last_row + 1))
End Sub

enter image description here


Solution

  • Sub title_full()
        Dim last_row As Long, ws As Worksheet
        
        With Sheets("A")    ' next statements such as Sheets("A").Range("A4") written as .Range("A4")
            .Cells.Delete   ' clear the assignment sheet (optionally)
            
            Sheets("Gennaio").Range("A5:N36").Copy Destination:=.Range("A4") 'initial copy with headers
            
            For Each ws In Sheets(Array("Febbraio", "Marzo"))   ' the array may be extended to other months
                last_row = .Cells(Rows.Count, 1).End(xlUp).Row  ' determine last_row in the sheet "A" before each copy
                ws.Range("A6:N33").Copy Destination:=.Range("A" & (last_row + 1))
            Next ws
            
        End With
    End Sub