+ Reply to Thread
Results 1 to 6 of 6

VBA code to make multiple text boxes numeric entry only

  1. #1
    Registered User
    Join Date
    05-10-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    11

    VBA code to make multiple text boxes numeric entry only

    Hello

    Im very new to VBA coding and just looking for some help with short cuts.

    I have a large userform used to record productivity data in my workplace. I have 167 text boxes (named Input1 up to Input167) and I want them to only be able to enter in numbers.


    I have the following code which works fine, but I really dont want to have to repeat it 167 times. Can someone help me with some code so that I can cover all 167 text boxes at the one time.

    Please Login or Register  to view this content.
    Thanks in advance for your help

    Jess
    Last edited by vlady; 05-19-2013 at 10:49 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA code to make multiple text boxes numeric entry only

    Hello JAL_0110,

    Welcome to the Forum!

    This can be done easily using a technique known as "Subclassing". This allows for objects to be grouped together and share common event routines.

    Before you proceed, have your workbook open and the VB Editor activated. To activate the VB Editor, press the keys ALT+F11.

    Adding the Class Module and Code
    1. To activate the VB Editor, press the keys ALT+F11.
    2. Copy the Claas Code below using Ctrl+C.
    3. Add a new Class Module to your VBA project. Press the keys [b]ALT+I[/] to display the Insert menu. Press C key.
    4. Paste the code into the module using Ctrl+V.

    Class Module Code
    Please Login or Register  to view this content.
    Additional UserForm Code
    Add the code below to the very top of your UserForm...
    Please Login or Register  to view this content.
    Add the code below to the UserForm_Initialize() Event Module...
    Please Login or Register  to view this content.
    If you have any problems, post your workbook and I will fix any problems you are having.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-10-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code to make multiple text boxes numeric entry only

    Hi Leith Ross

    Thanks so much for your assistance. I followed your instructions but it came up with an error in Private Sub UserForm_Initialize():


    Compile Error: User-Defined Type Not Defined
    (on Set TB = New clsTextBox line)


    I have attached the file. As you will see when you open it, there are a number of userforms being constructed. Please go to AddProductivityData userform.

    Thanks Again for your help. It is greatly appreciated.

    vsa productivity userform TEST.xlsm

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA code to make multiple text boxes numeric entry only

    Hello JAL_0110,

    Sorry about that, I forgot to edit that line after testing the code on my machine. It is a simple fix. The attached file has the macro change.

    Change the line below
    from this...
    Please Login or Register  to view this content.
    To this...
    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    05-10-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code to make multiple text boxes numeric entry only

    Hello

    Was getting a

    Run-time error '13'
    Type mismatch

    (as i had put first textbox as input1, where first actual textbox was input5)

    I have amended and is running like a dream.


    Thanks so much for your help.
    Last edited by JAL_0110; 05-20-2013 at 02:02 AM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA code to make multiple text boxes numeric entry only

    Hello JAL_011,

    I discovered that error after I posted the first fix. This error is happening because you are referencing TextBoxes that are not on the UerForm. Change the loop to start at 5 instead of 1 in the Initialize event and it will work. I just checked it to be sure.

    Working UserForm_Initialize Code
    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