+ Reply to Thread
Results 1 to 6 of 6

VBA User Form is adding data to wrong row

  1. #1
    Registered User
    Join Date
    02-07-2023
    Location
    Wisconsin, USA
    MS-Off Ver
    Microsoft Office 2016
    Posts
    6

    VBA User Form is adding data to wrong row

    Hey there,

    I'm trying to create a macro for a user form so when the user fills in customer information and clicks submit, it will add a customer name and time stamp to a "customer list" table.
    There will eventually be more than the customer name and time, once I get the rest of the code working properly.

    It is also set to add a row to the table before adding the customer, so I never run out of rows. Not sure if that has anything to do with my issue.

    Every time I run it, it adds the customer to the bottom row (the new row that it added) and still leaves empty rows above it.

    I suppose it doesn't have to add a row if there is already an open row available, but the main issue is the customer should always be added to the first empty row, not the very last row.

    I prefer to not upload the entire spreadsheet because there is some confidential info on it, but here is what I can share...

    I attached a screenshot to that portion of the sheet. The code should've inserted Customer 2 into row 17 as it is the first empty row, but instead, it inserted them into row 20, the last empty row.


    Here is the code I have right now for when the user clicks submit on the user form:

    Please Login or Register  to view this content.
    I appreciate any info you can share!
    Attached Images Attached Images
    Last edited by cody1p3; 04-30-2023 at 09:36 AM. Reason: screenshot correction

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

    Re: VBA User Form is adding data to wrong row

    I have attached a very basic userform to demo methods of save/update/delete using a 'structured table'.
    first a few 'does & don'ts' the principal of the 'structured table' is that it self maintains - you do not add blank rows to the table in anticipation - this is catered for by using the appropriate table handling references.
    Do not use sheet range size handling with tables - use the right referencing methods.
    To use the demo - first press 'Clear' input you new data -press 'Add' to transfer to sheet - to 'Update' or delete first select with the combobox.
    You will see the UserForm controls are left with their default names - this enables looping through rather than individually referencing each textbox - if your app had 50 textboxes it would not be any larger than this demo using this method.
    I have included a PDF of the code structure that you may find useful to refer to - hope some of this is of assistance.
    Attached Files Attached Files
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  3. #3
    Registered User
    Join Date
    02-07-2023
    Location
    Wisconsin, USA
    MS-Off Ver
    Microsoft Office 2016
    Posts
    6

    Re: VBA User Form is adding data to wrong row

    Thanks for all your effort with this and the quick response!

    I'm not experienced with this so I'll take a closer look over the next couple days and let you know if I can understand it!

    Thanks again!

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA User Form is adding data to wrong row

    Hello. Two comments:

    a) Never -but never- leave empty rows in a listObject: Consider it a crime! (Ja ja ja).

    b) Regarding the VBA code that you have shown us, I would say that it is a bit complicated without need. I show you a much simpler way:


    PHP Code: 
    Private Sub Submit_Button_Click()
    Dim TableListObject As ListObject

    Set TableListObject 
    Sheets("Daily Goal and Sale Tracker").ListObjects(1)
    With TableListObject.ListRows.Add(1)
      .
    Range(1) = Now
      
    .Range(2) = Name_Text
    End With

    MsgBox 
    "Customer Added Successfully!"
    End Sub 
    _______________________________

    I forgot to comment:

    If you've bound this listObject to a listBox, for example, then you should add two lines to the above:

    Please Login or Register  to view this content.
    Last edited by beyond Excel; 04-30-2023 at 01:01 PM.
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  5. #5
    Registered User
    Join Date
    02-07-2023
    Location
    Wisconsin, USA
    MS-Off Ver
    Microsoft Office 2016
    Posts
    6

    Re: VBA User Form is adding data to wrong row

    I got a lot of useful information from the files you attached, and I got my issue figured out! Thank you!!

  6. #6
    Registered User
    Join Date
    02-07-2023
    Location
    Wisconsin, USA
    MS-Off Ver
    Microsoft Office 2016
    Posts
    6

    Re: VBA User Form is adding data to wrong row

    I got a lot of useful information from the files you attached, and I got my issue figured out! Thank you!!

+ 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. User form not adding data to sheet
    By Ashleytaylor1702 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 12-10-2020, 06:15 AM
  2. [SOLVED] Adding data from user form to email
    By echoz in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-04-2017, 10:44 AM
  3. User Form Data Entry Adding a TimeStamp
    By caltman242 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2015, 09:15 AM
  4. [SOLVED] User Form not adding data
    By JO505 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 02-07-2013, 01:21 PM
  5. Adding "All Borders" with a User Form (Similar to Data Form)
    By kgreave1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-26-2012, 01:57 PM
  6. User Form List Boxes and Adding and Deleting Data
    By learning_vba in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-04-2010, 06:23 PM
  7. Adding data to sheet with user form
    By Jay in forum Excel General
    Replies: 1
    Last Post: 06-08-2005, 11:05 AM

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