+ Reply to Thread
Results 1 to 19 of 19

Trouble creating a UserForm programmatically

  1. #1
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Trouble creating a UserForm programmatically

    I am having issues with writing code that creates a UserForm on Excel 2013. I have searched Microsoft help sites as well as many other sites for solutions, and I have figured out that I don't have a problem with the coding, but it is a problem that I have no idea how to solve.

    I found a MS help website that walked me through how to create a UserForm using VBA code. It said to first click tools > references > check MS VBA Extensibility. I then copy and pasted their sample code, but it gave me the error: Run-time error '1004': Application-defined or object-defined error. I then went back to references and saw that MS VBA Extensibility's location is given as C:\Program Files (x86)\Common Files\Microsoft Shared\VBA. I went into the MS Shared folder, but didn't find VBA, so I copied it to my computer from another computer in my office. That did not fix the problem. I am wondering if I may be missing files? The only folder inside VBA is VBA6 which contains only VBE6EXT.OLB. I have tried running code that claims to create a UserForm, but I keep getting the same error. Here is an example of the code I have tried using.

    Sub Add_Form1()

    ' Declare a variable to hold the UserForm.
    Dim x As Object

    ' Create a new UserForm. You can use this new VBComponent object
    ' to manipulate the User Form.
    Set x = Application.VBE.ActiveVBProject.VBComponents.Add _
    (vbext_ct_MSForm)

    End Sub

    My error is on the line Set x = Application.VBE.....
    When I manually type in the code, VBA recognizes everything I am typing in (it will give me autofill options), so I am stumped as to why this code won't work for me.
    Last edited by Mitch319; 08-18-2014 at 11:56 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Trouble creating a UserForm programmatically

    Hi,

    The following code worked for me in Excel 2003:
    Please Login or Register  to view this content.
    I got the code from the basic file from the following URL:
    http://j-walk.com/ss/excel/tips/tip76.htm
    http://j-walk.com/ss/excel/tips/modGetOption.bas

    Lewis

  3. #3
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    Thanks, but I get the same error with that code. Earlier I tried running code that involves the VBE object, but it doesn't seem that it is recognized as an object. I think that relates to the problem, but I'm not sure how.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Trouble creating a UserForm programmatically

    The following reference may help:
    http://www.cpearson.com/excel/missingreferences.aspx

    My speculation is that you are missing the 'Microsoft Forms 2.0 Object Library' or the equivalent for your version of Excel.

    Lewis

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trouble creating a UserForm programmatically

    Eh, I've got to ask.

    Why do you want to create a userform programmatically?

    Anyway here's a (bad?) example of how to do it.
    Attached Files Attached Files
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    The reason I want to create a UserForm programmatically is I need am making a parts ordering system from work where guys from the shop can order parts without having to come to the office and bother the receptionist. There is a certain point in the process where the user inputs a number, then I need a window to pop up that has x amount of check boxes based on the number they input. Those check boxes will have different labels depending on the number inputted. I know of no way to do this without creating a UserForm that is updated programmatically. The problem is not so much creating the UserForm, but it is more like accessing any of the objects related to the UserForm. I will take a look at the rest of the answers when a get a chance and let you all know if they worked. Thanks!

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Trouble creating a UserForm programmatically

    You have to trust access to the VBA project in the macro settings in Trust Center.
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Trouble creating a UserForm programmatically

    If you need a varying number of checkboxes, you can create a Userform (at design time (now)) with a multi-select listbox whose .Style is set to fmListStyleOption.

    For a crude example, create a user form with a ListBox1 and put this code in a normal module.
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    Wow, I can't believe it was something that simple. Thanks romperstomper, that fixed it!

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Trouble creating a UserForm programmatically

    It's really really nasty doing it this way, you're much better off using Mike's approach or dynamically adding controls if it doesn't meet your requirements.

  11. #11
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Trouble creating a UserForm programmatically

    Mitch,

    See the attached file for an example of a static UserForm with dynamic controls (Test Boxes), including event handling for the dynamic controls.

    Lewis
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    Mike, your answer is exactly what I need. I just started using VBA (or any programming for that matter) about two weeks ago, so most of the info in these answers is over my head, but I do appreciate all the help!

  13. #13
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    I have a couple more questions about Mike's code. What does "Rem can be set at design time" mean? Am i supposed to replace that text with something? Also, how do I set the row source to a range that is selected based on the user's input? I have two variables, Cell1 and Cell2, that are used to select a range. I tried using (under With ListBox1) .RowSource = Range(Cell1,Cell2) but I get a type mismatch error.
    Last edited by Mitch319; 08-18-2014 at 12:15 PM.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trouble creating a UserForm programmatically

    It's a comment telling you that you can set the properties at design time rather than at run time in the code.

    As for the source for the listbox, how does the user input determine what that is?

    Will the user input/select an order when they open the form?

  15. #15
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    Yes Norie, the User selects an order number. I have code that selects a range of cells (Cell1,Cell2) based on that order number.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trouble creating a UserForm programmatically

    If you have code that selects the relevant range then it should be straightforward to populate the listbox.

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  17. #17
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Trouble creating a UserForm programmatically

    Quote Originally Posted by Mitch319 View Post
    ... What does "Rem ..." mean? ...
    Rem is an old, deprecated indicator that the line that follows is a comment.

  18. #18
    Registered User
    Join Date
    08-14-2014
    Location
    Cedar Falls, Iowa
    MS-Off Ver
    2013
    Posts
    11

    Re: Trouble creating a UserForm programmatically

    Thanks everyone. I wanted to avoid this, but I ended up creating a temporary worksheet that stores the data I need, then I copy the rowsource from there. It's not too fancy, but it meets my needs.

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trouble creating a UserForm programmatically

    Why not store the data you want in an array then use that to populate the listbox?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to programmatically add items to a userform combo box
    By dec789 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2012, 10:59 PM
  2. Creating values for list programmatically.
    By ballack in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2007, 04:30 AM
  3. add button to UserForm programmatically
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-01-2006, 08:10 AM
  4. [SOLVED] Create UserForm programmatically
    By Ber in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2005, 07:05 AM
  5. [SOLVED] Programmatically creating a map from data in Excel
    By David Brockus in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-08-2005, 05:06 PM

Tags for this Thread

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