0

我有一个看起来像这样的 excel 电子表格:

(C5): Top
(C6):
(C7):
(C8):
(C9):
(C10):
(C11):
(C12):
(C13):
(C14): Bottom

有 2 个用户定义的变量,apple 和 orange。假设 apple=3 和 orange=3。输出应如下所示:

(C5): Top
(C6): Apple 1
(C7): Apple 2
(C8): Apple 3
(C9): Orange 1
(C10): Orange 2
(C11): Orange 3
(C12): Hello world 1
(C13): Hello world 2
(C14): Bottom

如果 apple = 3 和 orange = 4,我希望输出如下所示:

(C5): Top
(C6): Apple 1
(C7): Apple 2
(C8): Apple 3
(C9): Orange 1
(C10): Orange 2
(C11): Orange 3
(C12): Orange 4
(C13): Hello world 1
(C14): Hello world 2
(C15): Bottom

如果 apple+orange >6,那么我想在 C14 上方插入空行,以便“Apple 1”到“Hello world 2”的行完全适合“Top”和“Bottom”行之间。

我该怎么做呢?我的代码如下。但是,当我使用“3”和“4”作为苹果和橙色的用户输入运行它时,它给了我这个错误:

“运行时错误‘1004’:应用程序定义的或对象定义的错误”

Private Sub CommandButton1_Click()

Dim apple As Integer, orange As Integer
Dim i As Long, j As Long, k As Long, l As Long, lRow As Long, sentence1 As Long, sentence2 As Long, addRows As Long

lRow = Cells(6, 3).End(xlUp).Row + 1
apple = InputBox("Please enter number of apples")
orange = InputBox("Please enter number of oranges")
sentence1 = 1
sentence2 = 1

fruit = apple + orange
addRows = fruit - 8
If fruit > 8 Then
    Rows("13:" & addRows).Insert Shift:=xlDown, _
        CopyOrigin:=xlFormatFromLeftOrAbove
End If

    For i = 1 To apple
        Cells(lRow, 3) = "Apple " & i
        lRow = lRow + 1
    Next i

    For j = 1 To orange
        Cells(lRow, 3) = "Orange " & j
        lRow = lRow + 1
    Next j
    
    For k = 1 To sentence1
        Cells(lRow, 3) = "Hello world 1"
        lRow = lRow + 1
    Next k
    
    For l = 1 To sentence2
        Cells(lRow, 3) = "Hello world 2"
        lRow = lRow + 1
    Next l
        
End Sub

 
4

1 回答 1

0

我重现了这个问题。

每次增加 lRow 时,它都会将结果向下移动一个。所以它从第 2 行开始,然后下一次运行它从第 3 行开始。

改变这个:

lRow = Cells(6, 3).End(xlUp).Row + 1

至:

lRow = 2

这只是为了摆脱第一个问题。您可以稍后处理该部分。

接下来我们需要记录以前的fruit值。当您再次单击该按钮时,您输入了较大的值,但您需要知道上次单击时“底部”所在的行。这样,您可以在将其插入顶部之前将其洗牌。

我无法完成每项更改,但我做的另一件重要的事情是录制宏(使用“开发人员”选项卡,您可以在 Excel 选项中启用它)。我录制的宏是选择一行并插入一个新行。我无法让你的代码工作。

'need a private member variable whose scope lives through out the module or a worksheet (beyond the life of a Dim variable in a Sub-routine or Function)
Private prevFruitCount As Integer

Sub Main()

Dim apple As Integer, orange As Integer
Dim i As Long, j As Long, k As Long, l As Long, lRow As Long, sentence1 As Long, sentence2 As Long, addRows As Long

lRow = 2
apple = InputBox("Please enter number of apples")
orange = InputBox("Please enter number of oranges")

'TO DO use strings instead of integers as returns for InputBoxes, and validate

sentence1 = 1
sentence2 = 1

fruit = apple + orange

addRows = fruit - prevFruitCount
If addRows > 0 Then
    For i = 1 To addRows
        'Rows("C:" & (fruit + sentence1 + sentence2 + 1)).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Dim rowToInsert As Integer
        rowToInsert = prevFruitCount + sentence1 + sentence2 + lRow
        Rows(rowToInsert & ":" & rowToInsert).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbov
    Next i
End If

'record the previous value of fruit, for the next click as a baseline
prevFruitCount = fruit

    For i = 1 To apple
        Cells(lRow, 3) = "Apple " & i
        lRow = lRow + 1
    Next i

    For j = 1 To orange
        Cells(lRow, 3) = "Orange " & j
        lRow = lRow + 1
    Next j
    
    For k = 1 To sentence1
        Cells(lRow, 3) = "Hello world 1"
        lRow = lRow + 1
    Next k
    
    For l = 1 To sentence2
        Cells(lRow, 3) = "Hello world 2"
        lRow = lRow + 1
    Next l
        

End Sub

使用“运行时错误'1004':应用程序定义的或对象定义的错误”,我能够在单步执行 VBA 代码并手动更改电子表格中的值时遇到该错误。

于 2020-11-18T03:54:05.053 回答