Current location - Loan Platform Complete Network - Local tax - In Excel VBA, I want to copy the contents of a range to another table, but the number of columns in the copied range is a variable. How should I write a program?
In Excel VBA, I want to copy the contents of a range to another table, but the number of columns in the copied range is a variable. How should I write a program?

Simplify it:

Dim i As Integer

i = 2

Range(Cells(1, 1), Cells(2 , i)).Copy Sheets(2).Range("a1")

If you want to open the Sheets(2) worksheet, add the statement at the end: Sheets(2).Activate

You can also use the following statement:

Range("a1").Resize(2, i).Copy Sheets(2).Range("a1")

Explanation :

The Resize property of the Range object is used to adjust the size of the specified area and return the adjusted cell area.

Syntax: Resize(RowSize,ColumnSize)

The RowSize parameter and the ColumnSize parameter respectively represent the number of rows and columns of the adjusted cell area