+ Reply to Thread
Results 1 to 26 of 26

Excel VBA: Add Row to end of Table error

  1. #1
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Excel VBA: Add Row to end of Table error

    Hi Guys,
    I've been programming for what seems ages and never ran into a similar issue.
    I haven't prepared the file to attach but I just want to ask if somebody ever ran into similar issues.
    I have one worksheet named Database an the variable in VBA is DBSheet
    The Table (Listobject) is named tbl_Data and the variable is DBTable


    The code below
    Please Login or Register  to view this content.
    Works perfectly and adds a row to the end of the Listobject and it returns the correct row number in the table, this table holds 33 rows and after adding 34, so thta's correct

    I also have a Userform and a button to add data

    Please Login or Register  to view this content.
    The moment I confiem and the code 'add_Row_2_Table' is run it thows an error unable to address the ListObjec bla bla and Excel just closes, no chance to press debug or anything, it triggers the error immediately

    Any ideas?

    I've rebuilt the table, it has 7 columns and the first column a formula to place the row number in the table
    the formula:
    Please Login or Register  to view this content.
    If I remove the formula the same happens so that's not it
    Worksheet is unprotected and unlocked.

    Any ideas would be welcome
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Excel VBA: Add Row to end of Table error


    Hi,

    difficult with a partial code, I can just see how to optimize your code just removing the useless but no relation with your issue …
    How many tables in worksheet DBSheet ?
    Last edited by Marc L; 05-28-2023 at 09:27 AM.

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

    Re: Excel VBA: Add Row to end of Table error

    attached demo form is the format that I have adopted for virtually all my 'run of the mill' userforms.
    in the sub 'SAVEDATA' if adding to the next bottom row of a table
    first use the 'CLEAR' button if adding data - then press 'ADD' after filling in textboxes.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Torachan,

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

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Excel VBA: Add Row to end of Table error

    Hi again Keebellah,

    Nice to make contact with you again

    I didn't create your entire UserForm (just the "CmdSave" CommandButton) and I commented-out the code associated with the TextBoxes.

    I didn't experience any errors when running the code in Office 2016, and an extra row was added to the Table as intended.

    As a complete "shot in the dark", does the error occur if you use "iRow = add_Row_2_Table()" instead of "Call add_Row_2_Table"? I know that you should be able to "Call" a function routine, but experience shows that "should" is not necessarily synonymous with "can".

    Regards,

    Greg M

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,032

    Re: Excel VBA: Add Row to end of Table error

    Do away with the Function...This has never failed me...Populates Col B to whatever
    No file to test...keep formula in Col 1
    Please Login or Register  to view this content.
    Last edited by Sintek; 05-28-2023 at 03:02 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Hi Greg,
    Thanks for looking, the thing is the code that adds the row works perfectly when invoked outside the user form.
    The whole thing goes haywire when the Userform is used.
    The file belongs to somewone I'm trying to help out too but this baffles me.
    I'll attach the file in a moment.
    Cheers,
    Hans

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    The ADD button remains disabled but that I figured out
    I'm attaching the file I'm working on in a moment, it's from someone I'm trying to help
    Thanks for looking an yout effort
    Cheers,
    Hans
    Last edited by Keebellah; 05-28-2023 at 04:10 PM.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Hi Guys,
    Here's the culprit and I cannot figure out why.

    Just open the userform add data and press ADD

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

    Re: Excel VBA: Add Row to end of Table error

    Hi Keebellah, Initialization of the form failed for me the setting of the worksheet and table should be - this is also wrong in the submit routine
    Please Login or Register  to view this content.
    following on from that the combobox loads failed - at this stage it is far past my bedtime so will look again tomorrow.
    Also had a 'deja vu' moment when I saw the database - so I have attached a solution I gave four weeks ago - maybe the listview did not work on the O.P. computer although I had no feedback.
    Attached Files Attached Files

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Hi, this OP asked another question in the My Online Training Hub forum about the display size and this database, that I fixed but then ran into the ADD error.
    Your file throws an error with the view that I'm misssing project or library
    I'll try your macro on the version I attached earlier and see what that does.
    Thanks

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,032

    Re: Excel VBA: Add Row to end of Table error

    Ignoring the obvious combobox population errors, Something tells me that book has become corrupt...My supplied code above which has never ever failed also errors as per your findings...

  12. #12
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    @sintek: just tested your macro in the file but the moment a listrow is added I get the same error and it it's gone.
    Please Login or Register  to view this content.
    Last edited by Keebellah; 05-29-2023 at 05:07 AM.

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,032

    Re: Excel VBA: Add Row to end of Table error

    Yes, that is the case...I copied your Worms controls to a new userform and all worked as it should...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Sintek; 05-29-2023 at 03:46 AM.

  14. #14
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Great. Thanks , but...
    I still cannot understand why the code I have to add a row when run outside the form adds the row without errors and the moment it's trigerred by the userform command it hangs

  15. #15
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,032

    Re: Excel VBA: Add Row to end of Table error

    I told you...Your workbook and Form had somehow become corrupt...Your code works as it should with my new workbook attached...
    But why would you want to revert to the old style when a more stable simplistic method has been offered...

  16. #16
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    @Sintek: I agree for 200% with you, but was and am still curious what caused the corruption and why it works outise the form and not inside the ofrm.
    Thanks for you sound advice and help, as always

  17. #17
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,032

    Re: Excel VBA: Add Row to end of Table error

    One of Excels many unsolved mysteries...I had a very similar issue a couple of years ago with listrows.add method on 2010 & 2013 versions

    @sintek:
    just tested your macro in the file but the moment a listrow is added I get the same error and it it's gone.
    PS...Was not my code...Perhaps someone elses...
    Last edited by Sintek; 05-29-2023 at 05:43 AM.

  18. #18
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Thumbs up Re: Excel VBA: Add Row to end of Table error

    Here's the file with your userform
    That's the only thing different. I removed the old worms for and inserted yours, renamend it and added some modifications. It works!!!
    The secrets of Excel and its ways of workings will remain a mystery

    Thanks again for looking at it

  19. #19
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Excel VBA: Add Row to end of Table error

    Hi again Hans,

    Well, it took a bit of head-scratching, but it seems that the problem is caused by the following line in the "Reset" routine:

    Please Login or Register  to view this content.
    . . . commenting-out this line seems to avoid the problem of Excel crashing.


    I've taken the liberty of rewriting your code to decouple the UserForm from the Workbook as much as possible - UserForms should not "know" anything about worksheet layouts etc., and similarly, workbooks should not "know" anything about UserForm controls etc.

    The attached version of your workbook uses the following code in a standard VBA CodeModule:

    Please Login or Register  to view this content.

    and the following code in the VBA CodeModule of the UserForm:

    Please Login or Register  to view this content.

    Hope this helps - I'd be interested to hear any comments you might have.

    Best regards,

    Greg M
    Last edited by Greg M; 05-29-2023 at 08:11 AM. Reason: Highlighted correction inserted

  20. #20
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Excel VBA: Add Row to end of Table error

    Hi again,

    Just a (very!) slightly better version of the "Show_Form" routine:

    Please Login or Register  to view this content.

    Regards,

    Greg M
    Last edited by Greg M; 05-29-2023 at 10:24 AM. Reason: Workbook added

  21. #21
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Hi Greg,
    No the error occurs when the routine is called from the command click to save the record.
    The add_Row_2_Table routine works in any other place but not whne the command button is clicked.
    I used Sintek's code and just replaced the userfrom and thta works but it still bugs me not to know the reason why Excel hangs and restarts

  22. #22
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Just looked at your file and it works .
    Works great and thanks for that.
    Will take it with me and see what actually causes the problem, Reset is not called until after the attempt is made to save the new record so am still not sure where the actual corruption lies.
    Thanks to you too for the tip and patience helping out.

  23. #23
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Excel VBA: Add Row to end of Table error

    Hi again Hans,

    Many thanks for the feedback and also for the Reputation increase - much appreciated!



    Reset is not called until after the attempt is made to save the new record

    . . . NO! The "Reset" routine is called as soon as the UserForm is initialised.

    My first "experiment" with your code was to comment-out the entire "Reset" routine, and when that appeared to prevent the crash from occurring I gradually restored the various lines until I found the one which caused the problem.


    In the attached version of the workbook you originally posted, the only changes I've made are to comment-out the line I mentioned earlier, and also the lines which populate the ComboBoxes (I don't have access to the "Unique" function in Excel 2016).

    With these modifications I've been able to add rows to the table without any problem.


    Regards,

    Greg M

  24. #24
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Excel VBA: Add Row to end of Table error

    Hmmm . . . puzzling-er and puzzling-er


    Executing the highlighted line in the following routine causes Excel to crash:

    Please Login or Register  to view this content.

    But replacing that line with the following one cures the problem (obviously the ColumnHead values are not displayed in this case):

    Please Login or Register  to view this content.

    Has anyone got a rational explanation for this???


    Regards,

    Greg M

  25. #25
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel VBA: Add Row to end of Table error

    Hi Greg,
    The reason you got an error running Reset is probably because the code uses the UNIQUE() function which works only with 365 or version 2021 and newer, I forgot to mention that when posting the file.
    Thanks again

  26. #26
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Lightbulb Re: Excel VBA: Add Row to end of Table error

    Hi Sintek and Greg,
    Even though the post is solved I wanted to share the last update where I included the Application Version Number check.
    Greg reported het got an error when running Reset and the reason in his case is he is runnineg version 2016 and I used the UNIQUE() function which only works with 365 or 2021 and up.
    I had run into this when helping a friend and found a code that detects the 'real' version numer using the registry entry.
    In this attached sample I include that check in the USerform to check if the application version is greater than or equal 2021 or 365 and the form is then populated with either the UNIQUE() function or the Dictionary as Sintek did.

    Maybe you can put it to use in other projects.

    Thanks again for all the help suggestions and tips, never too ol to learn

+ 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. 400 Error when trying to Paste into an Excel Table
    By eddieperu in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-15-2020, 12:20 AM
  2. How to fix #div/0 error in pivot table excel 2007
    By DimitraBoz in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-02-2020, 12:00 PM
  3. Error when importing table from PDF to Excel
    By Faiz2430 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-18-2017, 01:17 PM
  4. Importing excel spreadsheet into access table, getting excel row header error
    By ncsthbell in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-19-2010, 02:36 PM
  5. error in pasting table from excel to powerpoint
    By amid in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2009, 11:31 PM
  6. Excel 2003 Pivot Table Error
    By ernestgoh in forum Excel General
    Replies: 2
    Last Post: 01-08-2007, 09:38 AM
  7. Error Table For Excel Comparisons
    By ajocius in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-26-2005, 10:05 AM

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