0

Alright so I am quite new with programming and with Visual Basic.
I have these codes, and what I need my program to do is to count the number of input of the user. Different inputs so I have the inputbox looping and once the user enters the value "0" that's when the looping will stop and a messagebox would show up showing the number of times the inputbox looped (thus showing the number of data entered)

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)

So it runs but it won't loop and so the messagebox always shows 1

3
  • what is your question Commented Jan 13, 2014 at 8:16
  • And what is the problem with your code? Commented Jan 13, 2014 at 8:16
  • Well it runs but it won't loop so the message box always says 2. Commented Jan 13, 2014 at 8:18

2 Answers 2

1

show up showing the number of times the inputbox looped

You have the loop incorrect. Is this what you are trying?

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

If you are planning to accept Text as well for inputNumber then you will have to change Dim inputNumber As Long to Dim inputNumber As String and Type:=1 to Type:=2

Followup from comments

To show the number of non-zero inputs (which your question doesn't specify. It says number of loop), Change the above code to

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
Sign up to request clarification or add additional context in comments.

7 Comments

The code isn't right, the value of i will be off by 1 if they enter the value 0 the very first time.
Isn't that correct? 0 is an input to the inputbox? This is what the user said a messagebox would show up showing the number of times the inputbox looped
He wants to show the number of non-zero inputs. You code will be off by one ... pretty sure...
Maybe you are right but where in the question does he say show the number of non-zero inputs. He says number of times the inputbox looped
Yeah it was off by one, if I entered four values, it counts five. But thanks!
|
0

Try the following:

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)

2 Comments

You are right - removed that erroneous "next i". Happy to help.
@user3189380, if this answers your question, you should mark it as so, both to benefit you and the community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.