计算输入框Visual Basic上的输入

时间:2014-01-13 08:13:26

标签: vba for-loop inputbox

好吧所以我对编程和Visual Basic都很陌生 我有这些代码,我需要我的程序做的是计算用户的输入数量。不同的输入,所以我有输入框循环,一旦用户输入值“0”,循环将停止,消息框将显示输入框循环的次数(从而显示输入的数据的数量)

Dim inputNumber As Integer
Dim i As Integer

For i = 0 To inputNumber
    inputNumber = InputBox("Please enter a value")
    Do Until inputNumber = "0"
        inputNumber = i
        i = i + 1
    Loop
Next i
MsgBox(i)

所以它运行但它不会循环,所以消息框总是显示1

2 个答案:

答案 0 :(得分:1)

  

显示输入框循环的次数

你的循环不正确。这是你在尝试什么?

Sub Sample()
    Dim inputNumber As Long
    Dim i As Long

    inputNumber = 1

    Do Until inputNumber = 0
        inputNumber = Application.InputBox("Please enter a value", Type:=1)
        i = i + 1
    Loop
    MsgBox i
End Sub

如果您计划接受Text以及inputNumber,则必须将Dim inputNumber As Long更改为Dim inputNumber As String,将Type:=1更改为Type:=2

通过评论进行跟进

要显示非零输入的数量(您的问题未指定。它表示循环次数),请将上述代码更改为

Sub Sample()
    Dim inputNumber As Long
    Dim i As Long

    inputNumber = 1

    Do Until inputNumber = 0
        inputNumber = Application.InputBox("Please enter a value", Type:=1)
        If inputNumber <> 0 Then i = i + 1
    Loop
    MsgBox i
End Sub

答案 1 :(得分:0)

尝试以下方法:

Dim inputNumber as Integer = 0
Dim i as Integer = -1
do
    inputNumber = InputBox("Please enter a value")
    i = i + 1
Loop while inputNumber <> 0
MsgBox("Number of inputs: " & i)