我有一个看起来像这样的 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