Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Baltimore, MD, Maryland, USA
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learning The VBA Blues

    I am trying to teach myself VBA. So, I open EXCEL VBA help and copy the following simple example:
    Sub getname()
    '
    ' getname Macro
    ' Macro created 03/02/01 by Stephen Stollmack
    '
    Dim answer As String
    answer = MsgBox(Prompt:="What is your name?")
    If answer = Empty Then
    MsgBox Prompt:="You did not enter your name."
    Else
    MsgBox Prompt:="Your name is" & answer
    End If
    End SubThe message pops up asking for my name and I click OK (because I have not put the input box in yet). The next thing that pops up is "Your name is1" (without the quotes). Why don't I get the 2nd message? And where does the 1 come from?
    Is this the appropriate forum for such basic Basic questions? Does anyone know of a group (message board)open to people who are trying to teach themselves VBA. I think that seeing other questions from beginers would help me get over the initial 'hump' and not burden you all with dumb questions.
    Thanks,
    Stephen S

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Learning The VBA Blues

    Try

    Sub GetName()
    Dim answer As String
    answer = InputBox(Prompt:="What is your name?")
    If answer = Empty Then
    MsgBox Prompt:="You did not enter your name."
    Else
    MsgBox Prompt:="Your name is" & answer
    End If
    End Sub

    You need to use an Input Box to get the name

    Andrew C

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Learning The VBA Blues

    Hi Stephen,
    The reason you only get one message is because you have an if..then...else statement which can only ever follow one of the possibilities (answer is either Empty or it's not). The "Your name is 1" message gets the 1 from the fact that you clicked OK (in VBA the response vbOK that is returned is a constant equal to 1.) I hope that's not confusing?
    As for the 'basic' questions bit, I'd suggest posting them in the VB/VBA forum in the Lounge whenever you get stuck. There isn't a set level for the Lounge, it's just here to help people who have problems! I would also recommend getting a reference book or looking at some of the online tutorials such as <A target="_blank" HREF=http://www.vbatutor.com>http://www.vbatutor.com</A>
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •