+ Reply to Thread
Results 1 to 10 of 10

ListBox Code for VBA form

  1. #1
    Forum Contributor
    Join Date
    06-07-2018
    Location
    Newcastle Upon Tyne, England
    MS-Off Ver
    365
    Posts
    143

    ListBox Code for VBA form

    I'm currently creating an invoicing system using a VBA form which is going really well however, I am now stuck with what I am hoping to achieve next.

    Everything is working great in terms of adding data to the form and using the 'Add' command button to store the data on a worksheet, but I am now trying to implement the next function...


    1. A ListBox to then be able to use the 'Find', 'Amend' 'Delete' functions etc.


    My idea for this is to display all invoices in the ListBox at the bottom of the form with a scroll bar on the right hand side.

    I would like to then be able to click on whichever invoice necessary and then have the data displayed on the form for me to make amends/changes or even delete a row if needed (I already have the code for the Delete ComandButton but obviously this won't work until the form can load data back into the form fields so it knows which row is to be dleted).

    I'm only asking for a starting point or recommended tutorials

    Cheers!
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: ListBox Code for VBA form

    If you're populating the listbox from the invoices on your MasterData sheet, then you could use the index number of the selected item in the listbox to determine the row number. The listbox index numbers are zero-based. If you populate the listbox starting with row 2 on MasterData, then the .ListIndex property + 2 will return the row number of the selected item in the listbox.

    rownum = ListBox1.ListIndex + 2

    Just make sure you repopulate the listbox whenever you make a change to the sheet.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: ListBox Code for VBA form

    Hi,

    The way I'd do this is create a dynamic range name for your invoice list. e.g.

    lst_inv :
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    change the 7 to change the number of columns you see in the ListBox

    Set the RowSource property of the Listbox to lst_inv
    Set the ColumnWidth Property to say
    30 pt;60 pt;60 pt;60 pt;60 pt;60 pt - change the numbers as necessary to display the number of characters from each field.

    Use the

    Please Login or Register  to view this content.
    procedure to list the invoices.

    That's a starter for 10.

    I'd then use the click event of the listbox to identify the row that's been selected and use that that row number to index the row in the Master Data sheet that contains the details and populate the form fields with the data from that row.


    See attached
    Attached Files Attached Files
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    06-07-2018
    Location
    Newcastle Upon Tyne, England
    MS-Off Ver
    365
    Posts
    143

    Re: ListBox Code for VBA form

    Thanks both!,

    Richard, I really like the idea of using the dynamic range name for the ListBox, it works well.


    1. Do you know if it's possible to choose which of the columns display in the listBox or is it a case of it will always be A1 working its way along the columns

    2. Is it possible to make the top row of the ListBox Bold where the titles are so they stand out? I've tried to mess around with it but was all bold or all regular from what I was able to do.


    Cheers
    Davo

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: ListBox Code for VBA form

    My 2cents.
    Setting the Columnwidth of a certain Column to zero will not display that Column.
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Forum Contributor
    Join Date
    06-07-2018
    Location
    Newcastle Upon Tyne, England
    MS-Off Ver
    365
    Posts
    143

    Re: ListBox Code for VBA form

    Cheers Rudi!

    I'm trying to use the following code for the 'Delete' button but it only works if the rows have no data stored.

    Please Login or Register  to view this content.
    The 'Amend' button I'm trying to code so that the data can be updated rather than duplicate or add a new row/invoice.

    Any ideas?

    Je bent zeer vriendelijk
    Davo
    Last edited by davo3286; 07-10-2018 at 08:44 AM.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: ListBox Code for VBA form

    Select a row in ListBox and click Delete-button.
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    06-07-2018
    Location
    Newcastle Upon Tyne, England
    MS-Off Ver
    365
    Posts
    143

    Re: ListBox Code for VBA form

    Thanks as always Rudi,

    Anyone got an idea of how the 'Amend' button I'm trying to code would work cz at the moment the data is duplicating rather than just updating that row...

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: ListBox Code for VBA form

    This should do it.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    06-07-2018
    Location
    Newcastle Upon Tyne, England
    MS-Off Ver
    365
    Posts
    143

    Re: ListBox Code for VBA form

    Thanks Rudi,

    I wouldn't be anywhere near finished without your help with the code... much appreciated!


    1. Is it possible to use the form as the actual invoice or will it be better exported from the worksheet like I'm currently planning?


    I just though maybe I could use the form as a 'programme' use a 'process invoice' command button with only the fields relevant to the invoice exported. What's your thoughts?

    Cheers
    Davo

+ 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] VBA code to change a chart based on a selection from a listbox on a form
    By cinstanl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2016, 03:07 PM
  2. User Form ListBox - Find Records (ListBox Populating Issue)
    By LONeillSSC in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-20-2015, 12:06 PM
  3. Is it possible to do that using a Listbox Form?
    By Rech in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2014, 10:08 AM
  4. Form Control Listbox VBA
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2009, 05:36 AM
  5. Modification of listbox to listbox code
    By Sam S via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2005, 07:05 AM
  6. listbox value to a form
    By alexanderd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2005, 12:05 PM
  7. [SOLVED] vb6 form with listbox
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-02-2005, 07:05 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