我正在撞墙,试图找出为什么我的复制粘贴没有排成一行。我以为我的索引得到了正确的判断,但我仍然遗漏了一些东西。对于第一种情况的数据是正确的,但是对于第二种情况则是一次,而对于第三种情况则是两次。它最初分别是两行和四行,但我发现我需要更新我的计数器而不管哪个陈述是真的。
我的代码如下:
Dim FirstItem As Integer
Dim SecondItem As Integer
Dim Offsetcount As Integer
Dim Rowoffset As Integer
Dim true_offset As Integer
Dim myNum As Integer
Sheets("Sheet2").Activate
ActiveSheet.Range("C2").Select
FirstItem = ActiveCell.Value
SecondItem = ActiveCell.Offset(1, 0).Value
Offsetcount = 1
Rowoffset = 0
true_offset = 1
myNum = (Range("C" & Rows.Count).End(xlUp).Row)
'MsgBox myNum
Do While myNum > 1
true_offset = true_offset + 1
If FirstItem = SecondItem Then
Offsetcount = Offsetcount + 1
Rowoffset = Rowoffset + 1
SecondItem = ActiveCell.Offset(Offsetcount, 0).Value
Else
Set myactivecell = ActiveCell
Set myActiveWorksheet = ActiveSheet 'Do I need to declare this and the line below?
Set myActiveWorkbook = ActiveWorkbook
ActiveSheet.Range(ActiveSheet.Cells(true_offset - Rowoffset, 1), ActiveSheet.Cells(true_offset + 1, 1)).EntireRow.Select
Selection.Copy
Set new_workbook = Workbooks.Add
ActiveSheet.Paste
myActiveWorkbook.Activate
myActiveWorksheet.Activate
myactivecell.Activate
ActiveCell.Offset(Offsetcount + 1, 0).Select
If ActiveCell.Value = "" Then
myNum = 0
End If
FirstItem = ActiveCell.Value
SecondItem = ActiveCell.Offset(1, 0).Value
Offsetcount = 1
myNum = myNum - 1
Rowoffset = 0
End If
Loop
以下是一些示例数据的屏幕上限:
答案 0 :(得分:0)
好的,我会告诉你当前代码的快速修复。从此声明中删除+1
:
ActiveCell.Offset(Offsetcount + 1, 0).Select ' ^^^^
那就是说,你的代码确实需要完全重构。你抱怨“撞我的头撞墙”但是当你不遵循编程中的良好实践规则时,这是正常的结果。
Option Explicit
我希望这会有所帮助。