2017-09-26 126 views
0

因此,我有一个宏可以剪切并复制“未完成的订单”,并在数据(See previous post)下面插入这些行,然后在开放订单数据上面粘贴标题。目前宏写入的方式,如果没有开放的订单数据,它将标题一直写到第65k行。如果在标题删除标题下面没有数据

见下面的代码:

Dim LastRow, NewLast, MovedCount As Integer 
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row 'Find last Row 
NewLast = LastRow + 1 'NewLast tracks the new last row as rows are copied and pasted at the end 
    MovedCount = 0 
For I = 2 To LastRow 
    If Left(Cells(I, 4), 1) = "O" Then 'Copy the row, increment NewLast and paste at the bottom. 
     Rows(I).Cut 
     'LastRow = LastRow - 1 
     Cells(NewLast + 3, 1).Select 
     ActiveSheet.Paste 
     Rows(I).Delete 
     I = I - 1 'Since we deleted the row, we must decrement i 
     MovedCount = MovedCount + 1 'Keeps track of number of rows moved so as not to overshoot the original last line 

     End If 
    If I + MovedCount = LastRow Then Exit For 'Exit For loop if we reached the original last line of the file 
Next I 

'inserts a header for the open section 
Cells(1, 1).Select 
Selection.End(xlDown).Select 
nRowMax = Selection.Row 
Selection.Offset(1, 0).Select 
Selection.EntireRow.Copy 
Selection.End(xlDown).Select 
Selection.Offset(-1, 0).Select 
Selection.PasteSpecial xlPasteFormats 
ActiveCell.Select 
ActiveCell = "Open Orders" 
Application.CutCopyMode = False 

所以我的问题是,我不是怎能停止,如果没有开放的订单数据或删除头,如果在它周围有没有数据被复制的标题。

我正在考虑放置一个IF,如果没有头THAN删除标题下的数据。对不起,如果这是有点开放我觉得有一些方法可以去做这件事。

查看下面的图片,让您了解数据看起来像和不带未结订单。 enter image description hereenter image description here

回答

1

如果我正确地理解了这个问题,那么如果没有开放的订单,您不希望“开放订单”标题填充。您可以通过在if语句中嵌套代码的整个底部来实现此目的:

If MovedCount <> 0 Then 
    Cells(1, 1).Select 
    ... 
    Application.CutCopyMode = False 
End If