1

我正在尝试复制和粘贴一个范围,以创建一个 28 x 28 的数字网格“旋转”值,以便每次将该范围粘贴到下一列时,该范围向下移动一行,最后一个值“溢出”回到下一行的顶部,我已经做到了这一点,但我被溢出部分难住了(我是 VBA 的相对新手)

Sub Test()

Dim oRange As Range
Set oRange = ActiveSheet.Range("A1:A28") 

Dim i As Integer
For i = 1 To 28
    oRange.Copy
    oRange.Offset(i, i).PasteSpecial xlPasteAll
Next i

End Sub

我还需要复制和粘贴单元格的值和格式

希望大家帮忙谢谢

4

1 回答 1

1
Sub Test()

    Dim oRange As Range
    Dim startColumn As String
    Dim rangeStart As Integer
    Dim rangeEnd As Integer
    Dim cellCount As Integer
    Dim i As Integer

    startColumn = "A"
    rangeStart = 1
    rangeEnd = 28
    cellCount = rangeEnd - rangeStart + 1

    For i = 1 To cellCount - 1
        Set oRange = ActiveSheet.Range(startColumn & rangeStart & _
                                ":" & startColumn & (rangeEnd - i))
        oRange.Copy
        oRange.Offset(i, i).PasteSpecial xlPasteAll

        Set oRange = ActiveSheet.Range(startColumn & (rangeEnd - i + 1) & _
                                ":" & startColumn & rangeEnd)
        oRange.Copy
        oRange.Offset((-1 * cellCount) + i, i).PasteSpecial xlPasteAll
    Next i

End Sub

编辑:

在索引“i”处插入一个空白行:

Rows(i & ":" & i).Select
Selection.Insert Shift:=xlDown

在工作表顶部插入 5 行在索引 1 处插入一行 5 次:

For i = 1 To 5
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown
Next
于 2009-10-14T16:07:03.843 回答