Home > Back-end >  How to create an If loop statement when the user clicks cancel
How to create an If loop statement when the user clicks cancel

Time:10-18

  • I am trying to figure out how create an if statement/loop if the user clicks cancel and then presents a MsgBox "Do you really want to QUIT" and if the user says no then it keeps going onto the if statement if they click yes then it ends the entire statement.

Code:

Sub Conversion()
'creating a program that converts Celsius C to Fahrenheit F and vice versa

    Dim temp As String 'gives a varible a type
    temp = InputBox("Enter the Temperature followed by F for Fahrenheit or C for Celsius") 'tells the user to input a tempertaure value either in Celsius "C" to Fahrenheit "F"
    
    Dim tempNum As Double 'gives a varible a type
    tempNum = Val(temp) 'converts the string into actual numbers
    
    Dim f As Double 'gives a varible a type
    Dim c As Double 'gives a varible a type
    Dim Conversion As String 'gives a varible a type
    Conversion = InStr(1, temp, "c", vbTextCompare) 'looks to see if the user input has a C or F in the string
    
    
    If MsgBox("Continue?", vbYesNo   vbQuestion) = vbNo Then
        If MsgBox("Do you really want to QUIT", vbYesNo   vbQuestion) = vbYes Then
            Exit Sub
    
        MsgBox "still going..."
        End If
    Else
        If Conversion > 0 Then 'creates a if statement if the temperature is in C it will convert to F Else it will convert it to C
            f = ((9 * tempNum / 5)   32) 'formula that converts Celsius in Fahrenheit
            MsgBox "Temperature in Fahrenheit F is " & f 'message that comes after the user inputs values
        Else
            c = ((tempNum - 32) * (5 / 9)) 'formula that converts Fahrenheit in Celsius
            MsgBox "Temperature in Celsius C is " & c 'message that comes after the user inputs values
        End If
    End If
    
End Sub

CodePudding user response:

Application Input Box in a Do Loop

Sub TemperatureConversion()

    Const Prompt As String = "Enter the temperature followed by ""F"" " _
        & "for Fahrenheit or ""C"" for Celsius"
    Const Title As String = "Temperature Conversion"

    Dim iTemp As Variant
    iTemp = GetApplicationInputBoxText(Prompt, Title)
    
    Dim Question As Long
    Dim iString As String
    Dim Conversion As String
    Dim iNumber As Double
    
    Do
        
        Select Case iTemp
        
        Case False ' canceled
            
            Question = MsgBox("Are you sure you want to quit?", _
                vbYesNo   vbQuestion, Title)
            If Question = vbYes Then
                Exit Sub
            Else
                iTemp = GetApplicationInputBoxText(Prompt, Title)
            End If
        
        Case "" ' no input
            
            Question = MsgBox("Please enter a temperature. Try again?", _
                vbYesNo   vbQuestion, Title)
            If Question = vbYes Then
                iTemp = GetApplicationInputBoxText(Prompt, Title)
            Else
                Exit Sub
            End If
        
        Case Else ' input
            
            iString = Application.Trim(iTemp)
            
            If InStr(1, iString, "C", vbTextCompare) = Len(iString) Then
                Conversion = "C"
            ElseIf InStr(1, iString, "F", vbTextCompare) = Len(iString) Then
                Conversion = "F"
            Else
                Question = MsgBox("Please add ""C"" or ""F"" to the end. " _
                    & "Continue?", vbYesNo   vbQuestion, Title)
                If vbYes Then
                    iTemp = GetApplicationInputBoxText(Prompt, Title, iString)
                Else
                    Exit Sub
                End If
            End If
            
            If Len(Conversion) = 1 Then
                iString = Application.Trim( _
                    Replace(iString, Conversion, "", , , vbTextCompare))
                If IsNumeric(iString) Then
                    iNumber = CDbl(iString)
                    Exit Do ' the only exit
                Else
                    Question = MsgBox("Please enter a temperature. " _
                        & "Try again?", vbYesNo   vbQuestion, Title)
                    If vbYes Then
                        iTemp = GetApplicationInputBoxText(Prompt, Title)
                    Else
                        Exit Sub
                    End If
                End If
            End If
        
        End Select
 
    Loop
    
    Dim F As Double
    Dim C As Double
    
    Select Case Conversion
    Case "C"
        F = ((9 * iNumber / 5)   32)
        MsgBox "Temperature (" & iString & "°" & Conversion _
            & ") in Fahrenheit is " & F & "°", vbInformation, Title
    Case "F"
        C = ((iNumber - 32) * 5 / 9)
        MsgBox "Temperature (" & iString & "°" & Conversion _
            & ") in Celsius is " & C & "°", vbInformation, Title
    'Case Else
    End Select
    
End Sub


Function GetApplicationInputBoxText( _
    ByVal Prompt As String, _
    ByVal Title As String, _
    Optional ByVal Default As String = "") _
As Variant
    GetApplicationInputBoxText _
        = Application.InputBox(Prompt, Title, Default, , , , , 2)
End Function

CodePudding user response:

Like this:

Sub Conversion()
    'creating a program that converts Celsius C to Fahrenheit F and vice versa
    Dim temp As String 'gives a varible a type
    Dim res As Double, resType As String, tempNum As Double
    Dim Conversion As String
    
    Do
        temp = InputBox("Enter the Temperature followed by F for Fahrenheit or C for Celsius")
        If UCase(Trim(temp)) Like "#*[F|C]" Then 'some basic check...
        
            resType = ""
            tempNum = Val(temp) 'converts the string into actual numbers
            If InStr(1, temp, "c", vbTextCompare) > 0 Then
                res = ((9 * tempNum / 5)   32)
                resType = "Fahrenheit F"
            ElseIf InStr(1, temp, "f", vbTextCompare) > 0 Then
                res = ((tempNum - 32) * (5 / 9))
                resType = "Celsius C"
            End If
            
            If Len(resType) > 0 Then
                MsgBox "Temperature in " & resType & " is " & res
            Else
                MsgBox "No usable value entered!", vbExclamation
            End If
        End If
        
        If MsgBox("Continue?", vbYesNo   vbQuestion) = vbNo Then
            'if user confirms then exit the loop
            If MsgBox("Do you really want to QUIT", vbYesNo   vbQuestion) = vbYes Then Exit Do
        End If
   Loop
    
End Sub
  • Related