Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote