如果在标题删除标题下面没有数据

因此,我有一个macros,它可以剪切和复制“打开的订单”,并在数据下面插入这些行(参见前面的文章) ,然后在打开的订单数据上面粘贴一个头。 macros写入的方式,如果没有开放的订单数据,它将标题一直向下排到第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删除标题。 对不起,如果这是有点开放性,我认为有几个方法去做这件事。

看到下面的图片,让你知道什么样的数据看起来像和没有开放的订单。 在这里输入图像说明 在这里输入图像说明

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

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