每25%的总行插入空白行,将余数添加到最后一个季度

时间:2014-09-30 10:07:21

标签: excel vba

我想在每行的25%中插入一个空白行,将余数添加到最后一个季度。 这是迄今为止的代码:

Sub spilt()
Dim cnt As Long
Dim divisor As Long
cnt = Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
#
#I'm assuming the logic goes here?
#
divisor = cnt / 4
MsgBox divisor & remainder

End Sub

我只是不确定如何通过每25%添加一个空行来分割行,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

Sub t()

 Dim last_row As Range

With ActiveSheet
    Set last_row = .Cells.Find(What:="*", after:=.Cells(.Rows.Count, .Columns.Count), LookIn:=xlValues, lookat:=xlPart, searchdirection:=xlPrevious, searchorder:=xlByRows)
    cnt = last_row.Row
    div = cnt / 4 + 1

 Do
    i = i + Int(div)
    .Rows(i).EntireRow.Insert
Set last_row = .Cells.Find(What:="*", after:=.Cells(.Rows.Count, .Columns.Count), LookIn:=xlValues, lookat:=xlPart, searchdirection:=xlPrevious, searchorder:=xlByRows)


 Loop Until i > last_row.Row

End With

End Sub

答案 1 :(得分:0)

以下是解决方案:

Sub spilt()
Dim cnt As Long
Dim divisor As Long
cnt = Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
divisor = cnt / 4 + 1
Range("A" & (divisor)).EntireRow.Insert
Range("A" & (divisor + divisor)).EntireRow.Insert
Range("A" & (divisor + divisor + divisor)).EntireRow.Insert

MsgBox "Total rows " & cnt & "."
End Sub

可能不是最优雅的解决方案,但它可以按照我的意愿运作。