+ Reply to Thread
Results 1 to 27 of 27

VBA userform

  1. #1
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Question VBA userform

    Hello,

    I am trying to build an Userform for my excel worksheet. Hopefully I can explain the case clear enough for your understandings.

    The case (see excel worksheet attached):
    I have currently 6 columns which two of them are already filled with parameters. The Userform should be serve as tool for other persons.
    I want to have the column "Partname" (A) as listbox, the column "Quantity" (B) is a fixed number. Column C, D, E and F should be filled by the userform.

    I have tried to write the code by searching on the Internet and using Youtube video's, but unfortunately it is not working (yet)... As well I tried to add a code for saying "Hallo" if the worksheet opens, but this one is also not working

    Hopefully someone with more experience in Userforms can help me with this case.


    Thanks.
    Attached Files Attached Files
    Last edited by luukos97; 06-10-2020 at 02:27 PM.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA userform

    .
    Here is a small project for you to study and incorporate into yours :

    Please Login or Register  to view this content.
    See the attached workbook for the above.

    Also, see your attached with the amended code for initiating the "Hello" msgbox upon opening.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Option Explicit
    Private Sub btnCancel_Click()
    Unload Me
    End Sub

    Private Sub btnOK_Click()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim newRow As Long

    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1

    'The next two lines can be expanded as many times as needed for all the entry fields in your project

    ws.Cells(newRow, 1).Value = Me.txtFirstName.Value
    ws.Cells(newRow, 2).Value = Me.txtSurname.Value

    End Sub
    Sub CommandButton1_Click()
    Selection.EntireRow.Delete
    End Sub
    I do understand this code more or less. But I do not understand how to insert the Listbox option. In my case I have 5 already given partnames (Column A), this should be
    processed into the listbox option.

    Thanks for sending me the sample Logit, it gives a clear overview of the sample and how to use it for that Userform.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA userform

    .
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by Logit View Post
    .
    Please Login or Register  to view this content.
    This code is ending in an error at my excel worksheet. Besides, I do not prefer having a new row, it should fill the already cells in the rows where the partnames are located. That is the code I am looking for.

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA userform

    .
    Where is the error ?

  7. #7
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by Logit View Post
    .
    Where is the error ?
    It close the excel worksheet. I think because the VBA is want to add a row but the worksheet sees a table?

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA userform

    See if this does what you are seeking :

    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86
    Quote Originally Posted by Logit View Post
    See if this does what you are seeking :

    Please Login or Register  to view this content.
    Thank you Logit. I will test it tomorrow morning, I have just put my pc in sleep mode.

  10. #10
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by Logit View Post
    See if this does what you are seeking :

    Please Login or Register  to view this content.
    It works Logit! Thank you. Now I will try to adjust it a little more as I did some new inspiration during my sleep :P. Maybe I will come up with some questions later on. I will keep this thread up to date.

  11. #11
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Update:

    I have added to the code that if the value is filled and ENTER is pressed, the Textboxes will be cleared. Now I am trying to remove the Listbox Item if it is inserted into the worksheet (see attached).

    But it is not working yet, can somebody help me what is wrong in the code?

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by luukos97; 06-09-2020 at 03:19 AM.

  12. #12
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,337

    Re: VBA userform

    I noticed in your original post you had entered your data into a table.
    I therefore have to question why then not use table referencing instead of the cumbersome range referencing you are using.
    With tables they are elastic, you do not constantly have to check last row etc.
    Also once you have loaded the listbox there is no need to search the worksheet for matches as you already have the row reference within the listbox.
    e.g. the listbox.listindex (note compensate for this been zero-based) makes add/delete/update code a lot simpler and direct.
    Note the listbox is populated from the table array not the unstable rowsource.
    torachan.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by torachan View Post
    I noticed in your original post you had entered your data into a table.
    I therefore have to question why then not use table referencing instead of the cumbersome range referencing you are using.
    With tables they are elastic, you do not constantly have to check last row etc.
    Also once you have loaded the listbox there is no need to search the worksheet for matches as you already have the row reference within the listbox.
    e.g. the listbox.listindex (note compensate for this been zero-based) makes add/delete/update code a lot simpler and direct.
    Note the listbox is populated from the table array not the unstable rowsource.
    torachan.
    Who, this is cool Torachan, I am going to play and adjust a little bit. Thanks

  14. #14
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Okay, now I have problems with inserting the code to my own excel xlsm file :S

    Is it possible to convert a sheet into another xlsm file?

    Otherwise I think I will proceed with the solution of Logit, this one is almost working now... I think I was a little bit too enthusiatic, the code is with my knowlegde a bit complex haha

    Let me know
    Last edited by luukos97; 06-09-2020 at 07:08 AM.

  15. #15
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,337

    Re: VBA userform

    Is it possible to convert a sheet into another xlsm file?
    I am not sure what you want to do.
    I only submitted the solution as I could not understand why bother with a table and not use table referencing.
    Also the unnecessary search methods when the location of data is already known.
    Rather than cause confusion it would be best to follow your present path albeit rather disjointed.
    torachan.

  16. #16
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA userform

    .
    Is this what you are seeking ?

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by Logit View Post
    .
    Is this what you are seeking ?

    Please Login or Register  to view this content.
    Hi Logit,

    No, what I trying to add to the userform is vizualised in this video, See minute 9:00. :
    HTML Code: 

    For example in my case. When I have selected Partname "Rod" with the given times and press ENTER, the partname "Rod" should be cleared. But I tried to insert the code from the video but encouter the error that "x" was not defined...

    Hopefully this elucidate the request.

    Let me know

  18. #18
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: VBA userform

    .
    Sorry ... I am not certain how that would be accomplished.

    Someone else might be able to assist.

  19. #19
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    No problem. Thanks for your help in this thread

  20. #20
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,337

    Re: VBA userform

    Re-run the youtube clip again, the listbox is initially loaded from a second sheet (Sheet2) not Sheet1, Sheet2 stays intact all the time for reloading.
    Sheet1 has no initial content, this is loaded with the items been deleted from the listbox.
    An altogether pointless exercise in the real world.
    torachan.

  21. #21
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by torachan View Post
    Re-run the youtube clip again, the listbox is initially loaded from a second sheet (Sheet2) not Sheet1, Sheet2 stays intact all the time for reloading.
    Sheet1 has no initial content, this is loaded with the items been deleted from the listbox.
    An altogether pointless exercise in the real world.
    torachan.
    So if I have understood you correct, this video isn't actual deleting the options from the listbox but deleting the options in sheet 2?

    Then, is there a workaround for the demaning I have?

  22. #22
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,337

    Re: VBA userform

    No, No.
    Sheet2 remains intact all the time - it is the source of the data for the listbox.
    The data in the listbox is removed from the listbox as it is transfered to Sheet1.
    This can only be done until the listbox is empty - then the form will have to be reopened (initialised) then the listbox is repopulated.

  23. #23
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Please Login or Register  to view this content.
    I am trying to add this to my VBA but getting the following error: "Compile error: Expected variable or procedure, not module".

    The Listbox looks like this:
    Please Login or Register  to view this content.
    What am I missing?

  24. #24
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,337

    Re: VBA userform

    Upload your workbook, without the surrounding context it is impossible to give an answer.

  25. #25
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by torachan View Post
    Upload your workbook, without the surrounding context it is impossible to give an answer.
    Sorry Torachan.

    What I try now is to add information in table1 (at sheet2). This should be displayed in the Listbox at the userform.

    See attached.
    Attached Files Attached Files
    Last edited by luukos97; 06-10-2020 at 02:44 AM.

  26. #26
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,337

    Re: VBA userform

    You had confused VBA by naming the module the same as the procedure (Sub) that was been looked for.
    Also you need to populate the listbox via the UserForm initialization event.
    When you increase the listbox column count also increase the number of column widths.
    Even if they are set at zero the data will be present, a feature you will come to exploit as you gain experience.
    Attached Files Attached Files

  27. #27
    Registered User
    Join Date
    04-22-2020
    Location
    Amsterdam
    MS-Off Ver
    365
    Posts
    86

    Re: VBA userform

    Quote Originally Posted by torachan View Post
    You had confused VBA by naming the module the same as the procedure (Sub) that was been looked for.
    Also you need to populate the listbox via the UserForm initialization event.
    When you increase the listbox column count also increase the number of column widths.
    Even if they are set at zero the data will be present, a feature you will come to exploit as you gain experience.
    This one works properly for me Thanks Torachan, I have set this thread to solved.

+ 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] Userform inputs comparison with sheet cells and if true reset userform fields
    By aidan5800 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-08-2018, 04:51 PM
  2. [SOLVED] Save data from UserForm into Sheet (how to resolve runtime errors in UserForm tutorial)
    By eighty6 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-14-2015, 07:41 PM
  3. combining chart userform and data transfer userform into 1 userform
    By H_Kennedy in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-04-2014, 07:11 AM
  4. combining chart userform and data transfer userform into 1 userform
    By H_Kennedy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-03-2014, 12:28 PM
  5. [SOLVED] Changed userform combobox to listbox, unable to get userform to retrieve datasheet values
    By dragonabsurdum in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-29-2013, 01:38 PM
  6. [SOLVED] Open a userform from a userform, but preserve the info in the original userform
    By jfoerch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2013, 02:38 PM
  7. userform is large.....how to use scrollbar of userform to show all of the userform
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2010, 04:11 PM

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