user12312459
user12312459

Reputation:

GoTo is not defined - in VBA

When I on the step F8 click. then say it Goto is not defined. I try to make a inputbox with a messagebox that me the answer give. And I try also to make code when the values not correct is. See, you where I make a mistake in my VBA code:

Sub TwoNumbersinputbox()

Dim bytAnswer1 As String
Dim bytAntwer2 As String
Dim Range As Byte
Dim strNumber1 As String
Dim strNumber2 As String
[C3] = "Number1"
[C4] = "Number2"

Start1:
strNumber1 = InputBox("Give number one?", "Invoer", 5, 567, 567)
   If IsNumeric(strNumber1) Then
   MsgBox "This must be Number1", vbCritical, _
   "Number1 input"
   GoTo strNumber1
   Else: [B2] = strNumber1
   End If

  If Not IsNumeric(strNumber1) Then
  MsgBox "there is error.", vbCritical, "Number2 input"
  bytAnwer1 = MsgBox("Start Again?", vbYesNo)
  If bytAnwer1 = vbYes Then GoTo Start
  End If

Start2:
strGetal2 = InputBox("Give Number2?", "Input", 5, 567, 567)
   If IsNumeric(strNumber2) Then
   MsgBox "This must be Number2 ", vbCritical, _
   "Number2 input"
   GoTo strNumber2
   Else: [B3] = strNumber2
 End If

  If Not IsNumeric(strGetal2) Then
  MsgBox "Is there an error.", vbCritical, "Number2 input"
  bytAnswer2 = MsgBox("Start Again?", vbYesNo)
  If bytAnswer2 = vbYes Then GoTo Start
  End If

End Sub

Upvotes: 1

Views: 230

Answers (1)

Vityata
Vityata

Reputation: 43585

  • First thing first - never use GOTO. Only in error handling (On Error statement (VBA)).
  • Second - if you need to use it, a mark is needed. E.g., if it is GoTo somewhere, then in the code it should be defined like this - somewhere:.

Sub DontUseGoTo()

    Dim i As Long
    i = 0

somewhere:
    i = i + 1
    Debug.Print i
    If i < 10 Then
        GoTo somewhere
    End If

End Sub

enter image description here

Upvotes: 3

Related Questions