【问题标题】:VBA cut-paste range of dataVBA剪切粘贴数据范围
【发布时间】:2016-10-07 14:24:40
【问题描述】:

我正在尝试使用 VBA 在 Excel 中制作大量遗传数据,使其更清晰易读。我试图在第 15 列之后剪切和粘贴每 7 个有数据的单元格,并将它们放在第 8-15 列下。 我需要的示例在包含的图片中。 DATA EXAMPLE (RAW vs What I need it to look like) 正如您从代码中看到的那样,真实数据要大一些。 (680 行和 100 多列。)

当我尝试运行代码时,将数据范围粘贴到新行时失败。 (错误 1004)

我的代码是:

Sub ShiftRows()

    Dim codingCol, startCol As Integer

    Dim LastRow As Integer
    Dim CurrentRow As Integer
    Dim LastCol, BeginCol As Integer
    Dim CurrentInsertRow As Integer


    LastRow = 2

    For CurrentRow = 680 To LastRow Step -1
        LastCol = 15
        Do While Cells(CurrentRow, LastCol) <> ""
        LastCol = LastCol + 1
        Loop

    CurrentInsertRow = CurrentRow

    For BeginCol = 0 To ((LastCol - 15) / 7) - 1

        CurrentInsertRow = CurrentInsertRow + 1
        Rows(CurrentRow).Offset(1).Insert shift:=xlShiftDown
        Range(Cells(CurrentRow, 15 + (BeginCol * 7)).Address & ":" & Cells(CurrentRow, 15 + (BeginCol * 7) + 6).Address).Cut
        Range("H:N" & CurrentInsertRow).Paste

        Next BeginCol
    Next CurrentRow
End Sub

【问题讨论】:

  • 你有没有想过这个问题?我在下面建议的答案有帮助吗?

标签: vba excel


【解决方案1】:

Range.Cut 允许您在其后指定粘贴范围。尝试类似:

Range("A1:A3").Cut Range("B10")

您将我的范围值替换为您想要的值。

【讨论】:

    【解决方案2】:

    您的单元格引用是"H:N" &amp; CurrentInsertRow,例如H:N2,这是不完整的。试试:

    Range("H" & CurrentInsertRow & ":N" & CurrentInsertRow).Paste
    

    【讨论】:

    • 首先,谢谢您的回答!然而,返回错误:对象不支持此属性或方法..:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多