【问题标题】:Copy and Paste row by index number in Excel Macro在 Excel 宏中按索引号复制和粘贴行
【发布时间】:2016-06-16 08:15:32
【问题描述】:

当满足某个条件时,我试图按索引号复制整行并将其粘贴到具有不同索引号的另一行(我知道问题不在于条件逻辑)。我正在考虑这样的事情:

Sub Makro1()

Dim i As Integer

With ActiveSheet
    'for looping
    totalRows = .Cells(.Rows.Count, "A").End(xlUp).Row

    'index of last row even after rows have been added
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    'data starts at row #3
    For i = 3 To totalRows
        If .Cells(i, 19).Value > 0 Then
            Number = .Cells(i, 19).Value
            Do While Number > 0
                lastRow = lasRow + 1
                'Next line doesnt do anything
                .Rows(lastRow) = .Rows(i).Value
                Number = Number - 1
            Loop
        End If
    Next i
End With
End Sub

逻辑按预期工作,但没有粘贴任何行。我一步一步走,确定问题不在于逻辑。

【问题讨论】:

  • TotalRows 和 LastRow 变量是否为您提供了您期望的值?
  • 另外,Lastrow = lastRow + 1 行中似乎有错字。不清楚这是否导致您的代码错误(如果您在模块开头不使用Option Explicit,则不会看到
  • totalrows 和 lastrow 好像是同一行???
  • @Nathan_Sav 它应该以相同的方式开始,但在 OP 循环期间递增

标签: vba excel macros


【解决方案1】:

我假设您要复制 Rows(i) 并将其作为值粘贴到 Rows(lastRow) 中。所以,你需要替换这一行

 .Rows(lastRow) = .Rows(i).Value

这两行:

.Rows(i).Copy
.Rows(lastRow).PasteSpecial xlPasteValues

或者

.Rows(lastRow).Copy
.Rows(i).PasteSpecial xlPasteValues

如果您想复制Rows(lastRow) 并将其作为值粘贴到Rows(i)。

编辑:

要粘贴所有内容(公式 + 值 + 格式),请将粘贴类型设为 xlPasteAll。

参考:msdn

【讨论】:

    【解决方案2】:

    范围复制和粘贴

    语法

    Range().复制[目标]

    方括号表示Destination 是可选参数。如果您不指定目标范围,它会将所选内容复制到剪贴板。否则,它将第一个范围直接复制到新位置。

    改变这一行:

    .Rows(lastRow) = .Rows(i).Value

    收件人:

    .Rows(lastRow).copy .Rows(i)

    值得注意的是

    .Rows(lastRow).copy .Cells(i, 1)

    也可以。 Excel 将调整目标范围的大小以适应新数据。

    【讨论】:

    • 这里不需要Destination:= 调用吗?
    • @RGA Destination:= 调用是可选的。这两个都有效,例如.Rows(1).Copy Destination:=.Rows(2) 和 .Rows(1).Copy .Rows(2) 也一样。
    【解决方案3】:

    你的代码适合我

    所以只需在 .Rows(lastRow) = .Rows(i).Value 语句处添加断点,然后在即时窗口中查询所有相关变量值,例如:

    ?lastRow
    ?.Rows(lastRow).Address
    ?i
    ?.Rows(i).Address
    

    同时你可以

    • 在代码模块的最顶部添加 Option Explicit 语句

      这将迫使您声明所有变量,从而导致一些额外的工作,但您将获得对变量使用和拼写错误更多控制的回报,从而节省调试时间

    • 保持Long 类型的行索引的dim 变量,以处理高于32767 的行索引

    • 使用范围对象的Resize()方法避免内循环

    很像以下:

    Option Explicit
    
    Sub Makro1()
    
        Dim i As Long, totalRows As Long, lastRow As Long, Number As Long
    
        With ActiveSheet
            'for looping
            totalRows = .Cells(.Rows.Count, "A").End(xlUp).Row
    
            'index of row to add from
            lastRow = totalRows + 1 '<--| start pasting values one row below the last non empty one in column "A"
    
            'data starts at row #3
            For i = 3 To totalRows
                If .Cells(i, 19).Value > 0 Then
                    Number = .Cells(i, 19).Value
                    .Rows(lastRow).Resize(Number).Value = .Rows(i).Value
                    lastRow = lastRow + Number
                End If
            Next i
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多