Macro isn't executing properly (1 Viewer)

ryanstens

New member
Local time
Today, 02:22
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:22
Joined
Aug 30, 2003
Messages
36,118
You set Speed but test spd.
 

Ranman256

Well-known member
Local time
Today, 05:22
Joined
Apr 9, 2015
Messages
4,337
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.
 

Minty

AWF VIP
Local time
Today, 09:22
Joined
Jul 26, 2013
Messages
10,353
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

Top Bottom