vba

Get new result on each repeat


Sub lab1_5()
Dim displayText As String
Do

    number = InputBox("Write number: ")

    For i = 1 To 9
        number = number * 2
        displayText = displayText & " " & number
    Next i
    MsgBox displayText
    Dim answer As VbMsgBoxResult
    answer = MsgBox("KONIEC" & vbNewLine & "Chcesz spróbować jeszcze raz?", vbYesNo)
Loop While (answer = vbYes)
End Sub

If I write for example 2 it shows 2 4 8 16 etc.
After clicking yes and writing another number i have previous (2 4 8 etc.) and new numbers.

How can I have only new numbers?


Solution

  • You need to reset displayText to an empty string at the start of the do loop.

    Sub lab1_5()
    Dim displayText As String
    Do
    
    Number = InputBox("Write number: ")
    displayText = "" '<--- this line here
    For i = 1 To 9
        
        Number = Number * 2
        displayText = displayText & " " & Number
    
    Next i
    MsgBox displayText
    Dim answer As VbMsgBoxResult
        answer = MsgBox("KONIEC" & vbNewLine & "Chcesz spróbowac jeszcze raz?", vbYesNo)
    Loop While (answer = vbYes)
    End Sub