Search code examples
vbacopy-pastenonblank

vba - copy-paste the last n non-blank column in the next blank column - on multiple same-formatted sheets


  • I have 4 sheets that are identical in format (same number of columns with different data).
  • On each sheet, I would like to copy the 6 last non-blank columns and paste them in the next 6 blank columns.
  • that should be done on all 4 sheets at once (i.e. not running the macro sheet after sheet).

I found multiple pieces of the answer but I am struggling putting them together (I have no knowledge in VBA).

Thank you a lot


Solution

  • This code copy the last not-empty row (6 columns) and paste them to the next empty row (6 columns). However this code only work if your sheets name have the number 1 to 4 in the end. (Remember to change the sheetname in the code if your sheet names are not "sheet1","sheet2","sheet3" and "sheet4") Hope it helps you or other (:

    Sub A()
    Dim count As Integer
    Dim sheetname As String
    
    sheetname = sheet
    
    For x = 1 To 4
    
    sheetname = "sheet" & x
    
    Worksheets(sheetname).Activate
    
    count = 1
    
    Do Until ThisWorkbook.Sheets(sheetname).Cells(count, 1).Value = ""
    
      count = count + 1
    
    Loop
    
    
    ThisWorkbook.Sheets(sheetname).Cells(count - 1, 1).Select
    
    
    ActiveCell.Resize(, 6).Copy
    
    ActiveCell.Offset(1, 0).PasteSpecial xlPasteValues
    
    Next x
    
    End Sub