Please help!

P

Paul H

I am trying to teach myself VBA and have just been pulling my hair out with
the following:

Public Sub name1()
Dim x As String
x = InputBox("Enter your name:")
If x = Paul Then MsgBox ("Hello, Paul")
ElseIf x <> Paul Then MsgBox ("Hello" & x)
End If
End Sub

Everytime I debug using F8 I get an error message saying Else without If. I
know I am only learning but surely there is an If at the start of line 4.
The help info is not much help as it seems to say the problem is before the
indicated problem, but I can't see it. So to stop an old man going mad can
you please help and explain what I am doing wrong.
 
S

Sandy Mann

Paul,
If x = Paul Then MsgBox ("Hello, Paul")
is a complete IF statement. Try:

Public Sub name1()
Dim x As String
x = InputBox("Enter your name:")
If x = Paul Then
MsgBox ("Hello, Paul")
Else: MsgBox ("Hello " & x)
End If
End Sub




--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

[email protected]
[email protected] with @tiscali.co.uk
 
D

Don Guillett

if ucase(x)="PAUL"
Public Sub name1()
'Dim x As String

x = If InputBox("Enter your name:")
If ucase(x) = "PAUL" Then
MsgBox "Hello, Paul"
Else
msgbox "Hello" & x
End If
End Sub

or why go thru all that just us

msgbox inputbox("Enter your name")
 
P

Paul H

Thanks Sandy and Don
Sandy That was a help I managed an ElseIf too (ooh from the croud). Don I
tried yours and it didnt like the x = If InputBox("Enter your name:") line
as it came up red and the first line attached itself to the previous
procedure. As I said I am very new to this all
Thanks again to you both no doubt I'll be back with more "silly" questions
Paul H
ps sorry about posting this in the wrong place and then repeating it under a
different subject.
 
D

Don Guillett

x = If InputBox("Enter your name:")
should be
x = InputBox("Enter your name:")

but, as I said, why not just use the simple one liner as the result is the
same.
msgbox inputbox("Enter your name")
 
P

Paul H

Hi Don
I am slowly working through a book "Excel Programming Weekend Crash Course"
and was having problems with a bit of code that had if and elseif
statements. I think what I was doing was not separating the Then from the
MsgBox or at least from the code that you guys have given me I think that's
what it was. What I have eventually get together is:

Public Sub name1()
Dim x As String
x = InputBox("Enter your name:")
If UCase(x) = "Paul" Then
MsgBox ("Hello, Paul")
ElseIf UCase(x) = "Don" Then
MsgBox ("Hello, Don")
Else: MsgBox ("Hello, " & x & " you old So and so!")
End If
End Sub
and put the Option Compare Text before the other procedures in the module
( I sound like I know what I'm talking about... I don't!!)
So I haven't come to the neat solution you suggested nor can I see how to
use it in an if / elseif situation.
Appreciate your help

Paul H
 
D

Don Guillett

try it. You get the same result in all three cases.
if paul=paul
if don=don
else =whatever name is in the input box which is the same result you get
with my one liner...............................
 
D

Don Guillett

btw yours won't work because the test was for Ucase
PAUL
DON
so you can't get a positive with
if ucase(whatever)="ucase"
because ucase is not UCASE
 
P

Paul H

Hi again
With respect Don my one does seems to work nicely here.
I thought Ucase treated any text as upper case so the code could compare it
and didnt require the original text to be in upper case so long as Option
Compare Text was placed before any procedures or did I read that wrong?
In fact I've tried mixing case and it still seems to work.
 
D

Dave Peterson

"Option Compare Text" will make that comparison non-case sensitive.

But I think your code would be confusing. I'd drop the ucase() function just so
that I wouldn't get confused later.
 
P

Paul H

Cheers, I am begining to find there are more than one way to get the desired
effect in VBA, which is going to make learning about it interesting.
Paul H
 
P

Paul H

Just tried Dave's suggestion to get rid of Ucase(). So the Option Compare
Text doesn't need Ucase() in the procedure for it to use text of any case
ahh!!
Thanks

Paul H said:
Cheers, I am begining to find there are more than one way to get the
desired effect in VBA, which is going to make learning about it
interesting.
Paul H
 
Top