简化R1C1代码

时间:2014-03-04 16:10:33

标签: excel-vba vba excel

如何简化以下代码?我想在可能的情况下添加变量并允许循环。

'3
Sheets("Sheet1").Cells(1, 5) = Sheets("Sheet1").Cells(1, 1)
Sheets("Sheet1").Cells(2, 5) = Sheets("Sheet1").Cells(1, 2)
Sheets("Sheet1").Cells(3, 5) = Sheets("Sheet1").Cells(1, 3)

Sheets("Sheet1").Cells(1, 6) = Sheets("Sheet1").Cells(1, 1)
Sheets("Sheet1").Cells(2, 6) = Sheets("Sheet1").Cells(2, 2)
Sheets("Sheet1").Cells(3, 6) = Sheets("Sheet1").Cells(2, 3)

Sheets("Sheet1").Cells(1, 7) = Sheets("Sheet1").Cells(1, 1)
Sheets("Sheet1").Cells(2, 7) = Sheets("Sheet1").Cells(3, 2)
Sheets("Sheet1").Cells(3, 7) = Sheets("Sheet1").Cells(3, 3)

'2
Sheets("Sheet1").Cells(1, 8) = Sheets("Sheet1").Cells(2, 1)
Sheets("Sheet1").Cells(2, 8) = Sheets("Sheet1").Cells(2, 2)
Sheets("Sheet1").Cells(3, 8) = Sheets("Sheet1").Cells(2, 3)

Sheets("Sheet1").Cells(1, 9) = Sheets("Sheet1").Cells(2, 1)
Sheets("Sheet1").Cells(2, 9) = Sheets("Sheet1").Cells(3, 2)
Sheets("Sheet1").Cells(3, 9) = Sheets("Sheet1").Cells(3, 3)

'1
Sheets("Sheet1").Cells(1, 10) = Sheets("Sheet1").Cells(3, 1)
Sheets("Sheet1").Cells(2, 10) = Sheets("Sheet1").Cells(3, 2)
Sheets("Sheet1").Cells(3, 10) = Sheets("Sheet1").Cells(3, 3)

感谢。

1 个答案:

答案 0 :(得分:0)

首先,您可以使用:

With Sheets("Sheet1")
    .cells(x,y) = .cells(a,b)
    ...
End With

为行和列索引使用“For ... Next”循环应该非常简单。