Macro isn't executing properly

ryanstens

New member
Local time
Today, 09:17
Joined
Oct 26, 2017
Messages
1
I need to create a macro that completes the following tasks.

1) Ask for the current car speed using an input box and store this value in a variable.

2) Using one If-Then-ElseIf structure output a message based on the speed.

This is what I have ...

Option Explicit

Sub question1()

Dim Speed As String
Speed = InputBox("What is your current car speed?")
Dim spd As Integer

If (spd < 0) Then
msgbox "this is an invalid speed"

ElseIf (spd > 0 And spd <= 54) Then
msgbox "You are driving too slowly"

ElseIf (spd >= 55 And spd <= 65) Then
msgbox "Your speed is fine"

ElseIf (spd >= 66 And spd <= 70) Then
msgbox "You are driving a little fast, but are probably okay"

Else
msgbox "You are driving too fast"

End If

End Sub


I keep getting "You are driving too fast" as my msgbox no matter what I input. What is the fix here? Thanks.
 
You set Speed but test spd.
 
If you use OPTION EXPLICIT in the module header,
then it will catch these variable name misspellings.
Otherwise its a mystery why the code wont work.
The eye wont always catch those spelling differences.
 
In fairness - the OP has used option Explicit and has declared both spd and Speed, just then used the wrong one.

As Speed is declared as a string I'm not sure the comparisons will work so he probably needs to add the line

spd = Val(Speed)

before it all kicks off?
 

Users who are viewing this thread

Back
Top Bottom