+ Reply to Thread
Results 1 to 11 of 11

UserForm - Editing data via TextBoxes from ListBox

  1. #1
    Registered User
    Join Date
    04-23-2020
    Location
    Galashiels, Scotland
    MS-Off Ver
    Office 2016
    Posts
    5

    UserForm - Editing data via TextBoxes from ListBox

    Hey Folks,

    Covid Lockdown has seen me create a new Excel system for my work, im all but done but come into 1 small issue, i wonder if you could point me in the right direction.

    I have a Userform that consists of;
    3 text boxes
    1 ListBoxand
    3 buttons (Add, Update, Delete)

    The ListBox source is a basic 3 column table (Employee Number, Name, Telephone Number) within the workbook. I have managed to get the data to show in the textboxes when i click on the row in the ListBox, and i can both add and delete rows in the ListBox, but the edit or "update" is where im struggling.

    I have the following code attached to my Update Button (which i got from another user online, iv just mastered most Formula and im new to the VBA scene)


    Please Login or Register  to view this content.
    When i click on a row in the ListBox, it come up in the text boxes as wanted, i can then change the data in "TextBox2" and click update and it will update in the ListBox and on the sheet, but for some reason it wont work with the other 2 TextBoxs

    Iv tried for 2 hours trying to edit it and test to see if it changes, but when i edit certain parts it just stops letting me update anything at all.

    Hope you guys can help

    Thanks in advance for supporting my evolving knowledge base

    Matt

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

    Re: UserForm - Editing data via TextBoxes from ListBox

    Please Login or Register  to view this content.
    If you change TextBox1, it will never match the sheet in column A and and find the correct row to change. It's hard to tell you how to fix it without seeing all your code.

    How do you populate the listbox?
    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
    Registered User
    Join Date
    04-23-2020
    Location
    Galashiels, Scotland
    MS-Off Ver
    Office 2016
    Posts
    5
    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    If you change TextBox1, it will never match the sheet in column A and change it.

    It's hard to tell you how to fix it without seeing all your code.

    How do you populate the listbox?
    I populate the listbox by putting the DNR in the rowsource in the listbox setting. I’m really out of my depth here 😂

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

    Re: UserForm - Editing data via TextBoxes from ListBox

    So if the row that is selected in the listbox is the one you are editing in the textboxes, you can use the listbox row number (the .ListIndex property) of the selected entry to figure out which row on the sheet to edit.

    This is a guess. I'm flying blind here without seeing your whole code.

    Change Listbox1 to suit.

    The + 2 corresponds to the 1st row of data on the sheet.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-23-2020
    Location
    Galashiels, Scotland
    MS-Off Ver
    Office 2016
    Posts
    5

    Re: UserForm - Editing data via TextBoxes from ListBox

    Hey Alpha,

    I tried your code, but it still only allows me to edit 1 feild (TextBox1)

    I coped this wb and shrank it to only this form and sheet in the hopes of uploading, but the forum wont let me as im new.

    All the code i have is below. Everything works apart from the update.

    Its frustrating as this 1 thing will complete the whole wb.

    Please Login or Register  to view this content.

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

    Re: UserForm - Editing data via TextBoxes from ListBox

    Sheet1 is it really Sheet1 or Sheets("Sheet1") - just grasping at straws - your code should work - have you seen the big yellow banner - how to upload your workbook.
    torachan.

  7. #7
    Registered User
    Join Date
    04-23-2020
    Location
    Galashiels, Scotland
    MS-Off Ver
    Office 2016
    Posts
    5

    Re: UserForm - Editing data via TextBoxes from ListBox

    Quote Originally Posted by torachan View Post
    Sheet1 is it really Sheet1 or Sheets("Sheet1") - just grasping at straws - your code should work - have you seen the big yellow banner - how to upload your workbook.
    torachan.
    Iv managed it

    It brings up a Compile error if i add the quotations and parentheses.
    Attached Files Attached Files

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

    Re: UserForm - Editing data via TextBoxes from ListBox

    It would appear that the 'accursed' rowsource was not co-operating, I do not now why as the code looked OK.
    to quickly prove it I have used a Table to load the listbox, and prove the 'update'
    I will look again tomorrow and alter the complete code to demonstrate the advantages of the Table approach.
    torachan.
    Attached Files Attached Files

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

    Re: UserForm - Editing data via TextBoxes from ListBox

    Quote Originally Posted by torachan View Post
    It would appear that the 'accursed' rowsource was not co-operating, I do not now why as the code looked OK.
    torachan is correct. RowSource is the culprit.

    So when the Update_Click procedure updates the 1st cell in a given row (before the other two cells), the listbox immediately updates because of RowSource. This in turn triggers the ListBox2_Click procedure and that code refreshes the other two textboxes with their old values erasing any changes in those textboxes.

    This code will apply all three textboxes en masse to a row

    Please Login or Register  to view this content.
    I would agree with torachan in that the Listbox2.List = Array method to populate the Listbox is better
    Last edited by AlphaFrog; 04-24-2020 at 09:07 PM.

  10. #10
    Registered User
    Join Date
    04-23-2020
    Location
    Galashiels, Scotland
    MS-Off Ver
    Office 2016
    Posts
    5

    Re: UserForm - Editing data via TextBoxes from ListBox

    Quote Originally Posted by torachan View Post
    It would appear that the 'accursed' rowsource was not co-operating, I do not now why as the code looked OK.
    to quickly prove it I have used a Table to load the listbox, and prove the 'update'
    I will look again tomorrow and alter the complete code to demonstrate the advantages of the Table approach.
    torachan.
    Quote Originally Posted by AlphaFrog View Post
    torachan is correct. RowSource is the culprit.

    So when the Update_Click procedure updates the 1st cell in a given row (before the other two cells), the listbox immediately updates because of RowSource. This in turn triggers the ListBox2_Click procedure and that code refreshes the other two textboxes with their old values erasing any changes in those textboxes.

    This code will apply all three textboxes en masse to a row

    Please Login or Register  to view this content.
    What an absolute pain in the ***, iv probably over complicated whats needed here, but it will be alot more smooth and better looking (UI wise)

    Thanks guys, appreciated.

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

    Re: UserForm - Editing data via TextBoxes from ListBox

    As previous posting.
    Updated code facilitating Table data storage.
    Advantages - elastic - self-sizing - no need to constantly check size for each entry/deletion.
    Placing your declaration of variables at the head (not in each sub) makes them available throughout the form.
    e.g. the listbox.listindex is only read once and is then available to each sub.
    I have used the mouseup event - this allows you to scroll down just holding left mouse button down then choice is made on release of button.
    Attached Files Attached Files

+ 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 - filling textboxes from a ListBox Selection
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-02-2017, 08:35 AM
  2. Using Userform Listbox for data management/editing
    By JusticeEmpire in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-12-2016, 10:09 AM
  3. Replies: 0
    Last Post: 02-04-2015, 03:46 PM
  4. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  5. Userform populate listbox with search from multiple textboxes
    By chendysworld in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-12-2012, 11:12 AM
  6. [SOLVED] Populate listbox columns with entries from textboxes on userform
    By AndyE in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2012, 11:05 PM
  7. Populating UserForm TextBoxes from ListBox results
    By AGrace in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2010, 03:07 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