Excel 如何将列表打印到特定单元格(VBA)

Excel 如何将列表打印到特定单元格(VBA),excel,vba,Excel,Vba,我正试图根据用户输入打印以下内容(比如apple=2,orange=3): 但是,当我运行脚本时,Apple 1将输出到单元格C2。要将Apple 1打印到单元格C10,我需要修改什么 我的代码如下: 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 A

我正试图根据用户输入打印以下内容(比如apple=2,orange=3):

但是,当我运行脚本时,Apple 1将输出到单元格C2。要将Apple 1打印到单元格C10,我需要修改什么

我的代码如下:

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

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

    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

当您执行
End(xlUp)
时,您执行的操作与Ctrl+Up相同,因此您正在更改行。如果您想要第10行,那么只需执行
lRow=10

哇,非常感谢,这很有效。我不敢相信这有多简单。我猜我把“lRow=lRow+1”放在哪里,它已经将行向下移动了。
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

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

    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