+ Reply to Thread
Results 1 to 12 of 12

Create Pop-up Error

  1. #1
    Forum Contributor
    Join Date
    07-28-2009
    Location
    Orange County, CA
    MS-Off Ver
    Excel 2007
    Posts
    115

    Create Pop-up Error

    Hello,
    I'm reading up on and learning VBA slowly.

    I'm trying to create a pop-up error within a macro.
    The macro is as follows:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    if you notice i'm trying to prevent the ability for a user to enter in text into the inputbox, and if they do input text i want the macro to repeat from the first line until the "Fig" = a number.

    Can anyone help me out?
    Last edited by garretonufer; 08-11-2009 at 07:23 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Create Pop-up Error

    Maybe like this:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Create Pop-up Error

    Try this:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    07-28-2009
    Location
    Orange County, CA
    MS-Off Ver
    Excel 2007
    Posts
    115

    Re: Create Pop-up Error

    It works perfectly.
    Don't want to seem childish but can you explain it for me?
    Does the "invalid number" error only exist within a function?

    I revised my macro with
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Create Pop-up Error

    Take a look in VBE Help for InputBox Method and post back if you don't understand it. Type = 1 requires the user to input a number.

  6. #6
    Forum Contributor
    Join Date
    07-28-2009
    Location
    Orange County, CA
    MS-Off Ver
    Excel 2007
    Posts
    115

    Re: Create Pop-up Error

    Will do, thank you

    and thanks for your input JBeaucaire.

    As I mentioned I'm just learning this stuff and I enjoy seeing the different perspective on the same issue, while giving the same results.

  7. #7
    Forum Contributor
    Join Date
    07-28-2009
    Location
    Orange County, CA
    MS-Off Ver
    Excel 2007
    Posts
    115

    Re: Create Pop-up Error

    Shg:
    I understand how the type plays a factor in the input box.

    What I don't understand is why it didn't work when i tried to enter into my original macro as follows
    Please Login or Register  to view this content.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Create Pop-up Error

    You don't need to declare different variables for Num and Ans unless you need to keep them both for some reason. Your code doesn't indicate that you do. You can simply use:
    Please Login or Register  to view this content.
    And again, a separate variable to store the answer from the MsgBox is also not required, you can simply use if/then on Msgbox itself:
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    07-28-2009
    Location
    Orange County, CA
    MS-Off Ver
    Excel 2007
    Posts
    115

    Re: Create Pop-up Error

    Nevermind, it appears that it must be
    Num = Application.InputBox(Prompt=:"Enter any number.", Type:=1)

    not having the Application. makes all the difference

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Create Pop-up Error

    If you look at Help, there is an InputBox method (which I used, and accepts a Type argument) and an InputBox function (which you used, and which doesn't).

    As you're just starting to learn VBA, you should acquire a good habit at the outset, and start declaring your variables.

  11. #11
    Forum Contributor
    Join Date
    07-28-2009
    Location
    Orange County, CA
    MS-Off Ver
    Excel 2007
    Posts
    115

    Re: Create Pop-up Error

    Quote Originally Posted by JBeaucaire View Post
    You don't need to declare different variables for Num and Ans unless you need to keep them both for some reason. Your code doesn't indicate that you do. You can simply use:
    Please Login or Register  to view this content.
    And again, a separate variable to store the answer from the MsgBox is also not required, you can simply use if/then on Msgbox itself:
    Please Login or Register  to view this content.
    I don't understand. Does VBA automatically reallocate the value for a variable if used in different places throughout the VBA code?

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Create Pop-up Error

    Quote Originally Posted by garretonufer View Post
    I don't understand. Does VBA automatically reallocate the value for a variable if used in different places throughout the VBA code?
    Essentially, yes.

    If I declare the Num = 5, then I can re-fill the variable again later with an entirely new number, or, as I've shown, by doing math on the current value. When the math is complete, the result becomes the new value of Num.
    Please Login or Register  to view this content.
    And you can do that over and over, absolutely. Unless you NEED the different versions of Num, there's no reason not to reevaluate the variable, else you're just cluttering up your macro with lots of little variables. You CAN do that, of course, and it may even make more sense to you. But to me, if the value of Num is changing, it's still the value of Num in my mind and having it just change as needed as we go makes more sense to me, rather than trying to keep track of Num vs Ans vs Final, etc.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1