gpt4 book ai didi

vba - 在 Excel 宏中按索引号复制并粘贴行

转载 作者:行者123 更新时间:2023-12-02 14:31:16 25 4
gpt4 key购买 nike

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

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(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

关于vba - 在 Excel 宏中按索引号复制并粘贴行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com