+ Reply to Thread
Results 1 to 16 of 16

Userform Textbox Formula

  1. #1
    Registered User
    Join Date
    07-01-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    89

    Userform Textbox Formula

    Hi All,

    Was hoping someone could help out -

    On my userform, i have a TextBox that i would like to prepopulate with data when the form initializes..

    Usually, i use the simple code of..

    Please Login or Register  to view this content.
    But, i would like to know if it would be possible for a random number to generated instead?

    For example, if i was to just type the formula normally into exel, it would be...

    =RANDBETWEEN(1000,9999)

    But this wont work if i write it in VB. Can you tell me how i can get this formular, to run each time the form initializes to the TextBox is filled with a random number?

    Many thanks for your help in advance
    Last edited by techteam; 07-29-2011 at 09:42 AM.

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    Sure, have a look at the "Rnd Function" topic in your VBA helpfile. It shows you how to generate a random number between two values. Also, particularly note the remark about the Randomize function.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    07-01-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Userform Textbox Formula

    Thanks colin - that was very helpful..

    Its says to use this statment...

    tickettxt.Value = Int((9999 - 10000 + 1) * Rnd + 10000)

    But it just keeps returning 10000? it wont randomly pick a number between my range - am i doing something wrong?

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Textbox Formula

    Do you mean

    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    (1) You need to put the randomize statement before it (see my comment in my first post).
    (2) Your lower bound is wrong. It should be one thousand, not ten thousand.

    Please Login or Register  to view this content.
    Application.WorksheetFunction does not have a method called Randbetween.
    Last edited by Colin Legg; 07-29-2011 at 06:56 AM.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Textbox Formula

    Yes it does, certainly in Excel 2010

  7. #7
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    Not in 2003. Besides, calling VBA.Rnd is going to be quicker.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Textbox Formula

    The OP seems to be using 2010

  9. #9
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    True, and I'll take it as read that you are correct that it was added as a method after XL 2003 (I don't have XL2010 to hand to check). I have no idea why they would have done that though; I'm certain that calling VBA.Rnd will be more efficient than calling Application.WorksheetFunction.Randbetween(). Any thoughts why they might have done it, Roy?

  10. #10
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    Nevermind - I figured it out. It's part of the Analysis Toolpak functions in 2003, so it was integrated in 2007. My advice is still to go with VBA.Rnd() though.

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Textbox Formula

    I think it was when they integrated the ToolPak into Excel's general functions. I can't remember whether this was 2007 or 2010.

    It run's pretty quick in a userform that I just created

  12. #12
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    think it was when they integrated the ToolPak into Excel's general functions. I can't remember whether this was 2007 or 2010.
    Yes, thanks. I realised that shortly after I asked.
    It run's pretty quick in a userform that I just created
    Okay, if I get a chance this weekend I'll run some tests in 2010 to see how the performance compares (I was going with my gut feeling).

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Textbox Formula

    I'm not saying it's quicker, but it would be interesting

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Userform Textbox Formula

    This is the sort of thing Colin does for fun anyway.
    Remember what the dormouse said
    Feed your head

  15. #15
    Registered User
    Join Date
    07-01-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Userform Textbox Formula

    Hi guys,

    Please Login or Register  to view this content.
    Worked fine - Many thanks for your help!

  16. #16
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Userform Textbox Formula

    In case anyone's interested, I just ran a little test. On my machine, in XL2010:
    Please Login or Register  to view this content.
    was, on average, roughly 40 times faster than:
    Please Login or Register  to view this content.

+ 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