我正在尝试使用 for then 循环复制每个小计并粘贴到每个数据集上方。也许在这里使用更合适的循环我不是100%肯定。在条件满足后,我无法在循环中复制小计。请参阅以下代码:
For I = 1000 To 2 Step -1 ' adjust 1000 to the row number of the last element
If Cells(I, 7).Font.Bold Then
Cells(I + 1, 1).Copy
Selection.End(xlUp).Select
Selection.Offset(1, -7).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next
我无法让宏观做的是复制实际小计,它甚至不复制它所选择的单元格。如果您需要查看我尝试获取循环的内容,请参阅下面的图片。
答案 0 :(得分:0)
Cells(I + 1, 1).Copy
正在正确复制单元格内容,但实际上并没有选择单元格。然后,您尝试通过从所选单元格开始移动选择来设置目标,但selection
的起点实际上并未由循环首先设置。
更改它的一种方法是在粘贴之前不要使用.select
。这也更有效,因为.select
效率非常低,应该最小化。
For I = 1000 To 2 Step -1 ' adjust 1000 to the row number of the last element
If Cells(I, 7).Font.Bold Then
Cells(I + 1, 7).Copy
Cells(I + 1, 7).End(xlUp).Offset(1, -6).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next