Vba 在Excel宏中按索引号复制和粘贴行
我试图按索引号复制整行,并在满足某个条件时将其粘贴到另一个索引号不同的行(我知道问题不在于条件逻辑)。我在想这样的事情:Vba 在Excel宏中按索引号复制和粘贴行,vba,excel,macros,Vba,Excel,Macros,我试图按索引号复制整行,并在满足某个条件时将其粘贴到另一个索引号不同的行(我知道问题不在于条件逻辑)。我在想这样的事情: 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
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
逻辑的工作原理与预期的一样,但没有粘贴任何线条。我已经一步一步地走了,并且确信问题不在于逻辑。范围复制和粘贴
语法
范围()。复制[目标]
方括号表示目标是可选参数。如果未指定目标范围,它会将所选内容复制到剪贴板。否则,它会将第一个范围直接复制到新位置
更改此行:
.Rows(lastRow)=.Rows(i).值
致:
.Rows(lastRow).copy.Rows(i)
值得注意的是
.Rows(lastRow).copy.Cells(i,1)
也会起作用。Excel将调整目标范围的大小以适应新数据。我假设您希望复制
行(I)
,并将其作为值粘贴到行(lastRow)
。所以,你需要更换这条线
.Rows(lastRow) = .Rows(i).Value
用这两条线:
.Rows(i).Copy
.Rows(lastRow).PasteSpecial xlPasteValues
或
如果要复制行(lastRow)
并将其作为值粘贴到行(i)
编辑:要粘贴所有内容(公式+值+格式),请使用粘贴类型作为
xlPasteAll
参考资料:您的代码适合我
因此,只需在.Rows(lastRow)=.Rows(i).Value语句中添加断点,然后在即时窗口中查询所有相关变量值,如:
?lastRow
?.Rows(lastRow).Address
?i
?.Rows(i).Address
与此同时,你可以
- 在代码模块的最顶端添加
语句 这将迫使您声明所有变量,从而导致一些额外的工作,但您将得到回报,对变量的使用和拼写错误有更多的控制,从而节省调试时间Option Explicit
- 调整变量的大小,以保存从
类型开始的行索引,以处理高于32767的行索引Long
- 使用范围对象的
方法避免内部循环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
选项显式
子Makro1()
Dim i为长,totalRows为长,lastRow为长,Number为长
使用ActiveSheet
“因为循环
totalRows=.Cells(.Rows.Count,“A”).End(xlUp).Row
'要从中添加的行的索引
lastRow=totalRows+1'是否totalRows
和lastRow
变量为您提供了所需的值?此外,lastRow=lastRow+1
行中似乎有输入错误。不清楚这是否导致了代码中的错误(如果在模块开始时不使用显式选项,则不会看到它。)看起来totalrows和lastrow将是同一行???@Nathan_Sav它应该是同一行,但在OP的循环过程中会增加,这是目标:=
调用不是必需的?@RGA目标:=
调用是可选的。这两种方法都有效,例如,.Rows(1)。复制目标:=.Rows(2)
和.Rows(1)。复制.Rows(2)
,并执行相同的操作。
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