Home » excel » excel vba – How to Avoid the Dreaded 'Select', 'Copy' & 'Paste' in this Example?

excel vba – How to Avoid the Dreaded 'Select', 'Copy' & 'Paste' in this Example?

Posted by: admin May 14, 2020 Leave a comment

Questions:

This works, but I know it is evil:

EIRPLLs.Select
Range(Cells(1, RevCol - 2), Cells(lastRevRow, RevCol + 3)).Copy
EIRPCases.Select
[A1:F1].Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues

The below is intended to be equivalent, but results in Method ‘Range’ of object ‘_Worksheet’ failed:

EIRPCases.Columns("A:F") = EIRPLLs.Range(Cells(1, RevCol - 2), Cells(lastRevRow, RevCol + 3)).Value

This also fails:

EIRPCases.Range(Cells(1, 1), Cells(lastRevRow, 6)) = EIRPLLs.Range(Cells(1, RevCol - 2), Cells(lastRevRow, RevCol + 3)).Value

What is the correct implementation?

How to&Answers:

I would do this one:

With EIRPLLs.Cells(1, RevCol - 2).Resize(lastRevRow, 6)
    EIRPCases.Range("A1").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With