Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 带循环的VBA输入框_Excel_Vba_Inputbox - Fatal编程技术网

Excel 带循环的VBA输入框

Excel 带循环的VBA输入框,excel,vba,inputbox,Excel,Vba,Inputbox,我刚刚开始学习VBA(和一般的编码),我面临一个问题,我还没有找到解决方案。我想创建一个带有循环的输入框,以便将输入框的输出打印到单独的单元格中。例如,我想将数字“5”写入输入框,输出将打印到单元格“A1”,下一个输入(如数字“9”)将打印到单元格“A2” 到目前为止,我已经设法做到了这一点,除了最后一排,一切都很好,因为我不知道如何从这里继续 Private Sub CommandButton1_Click() Dim myValue As Variant myValue = InputBo

我刚刚开始学习VBA(和一般的编码),我面临一个问题,我还没有找到解决方案。我想创建一个带有循环的输入框,以便将输入框的输出打印到单独的单元格中。例如,我想将数字“5”写入输入框,输出将打印到单元格“A1”,下一个输入(如数字“9”)将打印到单元格“A2”

到目前为止,我已经设法做到了这一点,除了最后一排,一切都很好,因为我不知道如何从这里继续

Private Sub CommandButton1_Click()
Dim myValue As Variant

myValue = InputBox("Please insert number")

Range("A1").Select
ActiveCell.Value = myValue

Range(ActiveCell) = Range(ActiveCell) + 1

End Sub
感谢所有帮助

请尝试以下代码

Private Sub CommandButton1_Click()
    Dim myValue As Variant
    myValue = InputBox("Please insert number")
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = myValue
End Sub
编辑#1: 根据用户3598756的建议更新了代码

Private Sub CommandButton1_Click()
    Dim myValue As Variant
    myValue = InputBox("Please insert number")
    If Range("A" & Range("A" & Rows.Count).End(xlUp).Row).Value = "" Then
        Range("A" & Range("A" & Rows.Count).End(xlUp).Row) = myValue
    Else
        Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = myValue
    End If
End Sub
试试下面的代码

Private Sub CommandButton1_Click()
    Dim myValue As Variant
    myValue = InputBox("Please insert number")
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = myValue
End Sub
编辑#1: 根据用户3598756的建议更新了代码

Private Sub CommandButton1_Click()
    Dim myValue As Variant
    myValue = InputBox("Please insert number")
    If Range("A" & Range("A" & Rows.Count).End(xlUp).Row).Value = "" Then
        Range("A" & Range("A" & Rows.Count).End(xlUp).Row) = myValue
    Else
        Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = myValue
    End If
End Sub

如果要执行循环(例如10次),则可以使用以下示例代码:

Sub CommandButton1_Click()

    Dim counter As Integer
    Dim myValue As Variant


    For counter = 1 To 10
        myValue = InputBox("Please insert number")
        Sheets("Sheet1").Cells(counter, 1).Value = myValue
    Next counter

End Sub

如果要执行循环(例如10次),则可以使用以下示例代码:

Sub CommandButton1_Click()

    Dim counter As Integer
    Dim myValue As Variant


    For counter = 1 To 10
        myValue = InputBox("Please insert number")
        Sheets("Sheet1").Cells(counter, 1).Value = myValue
    Next counter

End Sub

编辑到

  • 缩短代码

  • 如果单元格“A1”已填入标题,则添加解决方案

以下代码将执行以下操作:

Sub CommandButton1_Click()
    With Cells(Rows.Count, 1).End(xlUp)
        .Offset(IIf(.Value <> "", 1, 0)) = InputBox("Please insert number")
    End With
End Sub

编辑到

  • 缩短代码

  • 如果单元格“A1”已填入标题,则添加解决方案

以下代码将执行以下操作:

Sub CommandButton1_Click()
    With Cells(Rows.Count, 1).End(xlUp)
        .Offset(IIf(.Value <> "", 1, 0)) = InputBox("Please insert number")
    End With
End Sub

非常感谢你!这正是我要找的。@VilleT,那么你的问题有误导性,因为你从单元格“A1”开始向下,而这个解决方案跳过了单元格“A1”@user3598756。有意地,这是标题。@VilleT,永远欢迎。@KarthickGunasekaran在OP的问题中没有这种“目的”的迹象非常感谢!这正是我要找的。@VilleT,那么你的问题有误导性,因为你从单元格“A1”开始向下,而这个解决方案跳过了单元格“A1”@user3598756。有意地,这是标题。@VilleT,永远欢迎。@KarthickGunasekaran在OP的问题中没有这种“目的”的迹象谢谢你的回答谢谢你的回答