Copy 2 last columns and past n times

jocker_boy

Board Regular
Joined
Feb 5, 2015
Messages
83
Hello,

I found this code that copy last column and paste n times according to cell value.
How can i adapt to copy the last two columns and also past the same way?

And instead of reference the sheet name, is it possible to change to active sheet?

Thanks,
Gonçalo

VBA Code:
Sub YearsNumberReduction()

    Dim LastCol As Long
    Dim DelCnt As Integer
    DelCnt = Sheets("Panel").Range("E19").Value
    Dim copyCol As Range
    Dim DestRange As Range
    Dim lastRow As Long

    With ThisWorkbook.Sheets("Current")

        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        Set copyCol = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol))

        Set DestRange = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol + DelCnt))

        copyCol.AutoFill Destination:= DestRange, Type:=xlFillDefault

    End With
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Do you want to copy both columns the number of times in E19?
 
Upvote 0
In that case how about
VBA Code:
        Set copyCol = .Range(.Cells(1, LastCol - 1), .Cells(lastRow, LastCol))

        Set DestRange = .Range(.Cells(1, LastCol - 1).Resize(, 2), .Cells(lastRow, LastCol + DelCnt * 2))

        copyCol.AutoFill Destination:=DestRange, Type:=xlFillDefault
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top