In VBA, collect two strings from two different textboxes on the screen and send
ID: 3773303 • Letter: I
Question
In VBA, collect two strings from two different textboxes on the screen and send those two strings into a function. Create the function so that that will take in two strings. You will now convert the strings to numbers. MUST TRY TO CONVERT TO NUMBERS. If both values do not convert to numbers then return “Both values not numbers.”; if they both convert to numbers then return “Both values are numbers.”; if one of them can be converted to numbers then return “One value is a number.”. You will display the message you received from the function in a label on the form after it is returned from the function to the button click event.
Explanation / Answer
Sub DemoCInt()
Dim i As Integer, str1 As String
str1 = Range("A1")
i = ConvertToInteger(str1)
MsgBox i, , "Successful Conversion"
End Sub
Function ConvertToInteger(v1 As Variant) As Integer
On Error GoTo 100:
ConvertToInteger = CInt(v1)
Exit Function
100:
MsgBox "Failed to convert """ & v1 & """ to an integer.", , "Aborting - Failed Conversion"
End
End Function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.