0

我需要允许用户指定开始将空白行插入到他们的数据集中的行。查询的其余部分似乎工作正常。我只是不确定如何合并最后一个变量。这是我到目前为止的代码。

Dim NumRowsToInsert As Long
Dim RowIncrement As Long
Dim ws As Excel.Worksheet
Dim LastRow As Long
Dim LastEvenlyDivisibleRow
Dim i As Long

NumRowsToInsert = InputBox("How many rows would you like to insert between each 
row of data?")     'any number greater than 0
RowIncrement = InputBox("How many rows of data between line inserts?")       'ditto
Set ws = ActiveSheet
With ws
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    LastEvenlyDivisibleRow = Int(LastRow / RowIncrement) * RowIncrement
    If LastEvenlyDivisibleRow = 0 Then
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For i = LastEvenlyDivisibleRow To 1 Step -RowIncrement
        .Range(i & ":" & i + (NumRowsToInsert - 1)).Insert xlShiftDown
    Next i
End With
Application.ScreenUpdating = True
End Sub
4

2 回答 2

0

为什么不直接要求用户告诉您使用 InputBox 的哪一行,就像您对其他行所做的那样?我更改了您的代码以询问用户从哪一行开始,并将最后一个 for 循环设置为向上计数,而不是向 1 计数,而是向用户指定的行计数。您可能需要进行一些检查以确保用户没有指定低于文档末尾的行,但原则应该是正确的。我还没有测试过这段代码,所以不能保证!

Dim NumRowsToInsert,FirstRow As Long
Dim RowIncrement As Long
Dim ws As Excel.Worksheet
Dim LastRow As Long
Dim LastEvenlyDivisibleRow
Dim i As Long

Do
  FirstRow = InputBox("Please indicate at which row you want to start.")
Loop until isnumeric(FirstRow) 'keeps on asking until they enter an actual number, otherwise the program will lose its mind when the user inputs a character

NumRowsToInsert = InputBox("How many rows would you like to insert between each row of data?")     'any number greater than 0
RowIncrement = InputBox("How many rows of data between line inserts?")       'ditto
Set ws = ActiveSheet
With ws
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
LastEvenlyDivisibleRow = Int(LastRow / RowIncrement) * RowIncrement
If LastEvenlyDivisibleRow = 0 Then
    Exit Sub
End If
Application.ScreenUpdating = False
For i = LastEvenlyDivisibleRow To FirstRow Step -RowIncrement
    .Range(i & ":" & i + (NumRowsToInsert - 1)).Insert xlShiftDown
Next i
End With
Application.ScreenUpdating = True
End Sub
于 2013-10-15T12:44:37.417 回答
0

我想在 10 行数据中给定 4 和 1 行分隔线的增量,您希望第一组和第二组 4 行,在它们之间插入 1 个空白行,最后一组 2 行数据。这会在之前插入一行并完成其余的工作

For i = LastEvenlyDivisibleRow **+ 1** To 1 Step -RowIncrement

如果您不想插入第一行,则可以跳过循环的第一步:

For i = LastEvenlyDivisibleRow **+ 1** To 2 Step -RowIncrement
于 2013-10-14T18:05:50.790 回答