+ Reply to Thread
Results 1 to 26 of 26

Code to insert rows and lock command button

  1. #1
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Code to insert rows and lock command button

    Hi...Can anyone help with a code to insert a new row when a user starts to type in a cell. In the attached sample workbook when a user starts to type in cell A2 a new blank row should insert and the totals should include the new rows in the calculation. When the user starts to type in A3 then another new row should be inserted etc.

    I also have a macro button which I would like to lock so when new rows are inserted or deleted it does not affect the macro button.

    Thanks
    Attached Files Attached Files
    Last edited by fusion007; 07-05-2011 at 10:01 AM.

  2. #2
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Hi can anyone please help with this? Thanks

  3. #3
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    See if this works for you?

    .
    Attached Files Attached Files
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code to insert rows and lock command button

    I think you cant't add rows: in E2003 a worksheet always consists of 65536 rows.
    But you can change values in rows/cells.
    Use as formulae: =SUM(D$1:D3) and =SUM(E$1:E3)

    Then this suffices:

    Please Login or Register  to view this content.



  5. #5
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Thanks Mike. The code works well but I will want the insert to be on always rather than the user have to click on the toggle button. How can the code be amended so that rows are inserted always once the user starts to input in the first row? Thanks

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code to insert rows and lock command button

    You probably overlooked my suggestion...

  7. #7
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    No Problem,

    Here is an update: Post back any issues...
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Hi snb I did try your code but it had no effect. Can you please check it and let me know? Thanks

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code to insert rows and lock command button

    Enter something in A3; Enter
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Mike - your code works well and locks the macro button in place. At the moment the user has to either press enter or the down arrow in column A for a new row to be inserted before the totals row. Is there a way that as soon as a user starts to type something in a cell in column A a new row is inserted?

    SNB - your code works as well but the formatting of the lines (borders) in columns D and E is copied down each time a new row is inserted.

    Thanks

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code to insert rows and lock command button

    You have my permission to adapt that in my code.

  12. #12
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Hi...I've had to add a few more columns to the workbook and have amended the code slightly but the borders are not picking up correctly and the totals are no being picked up from the new rows inserted. I'm attaching the workbook. Could you please cast your eyes over the code and let me know if there is anything I need to amend. Thanks
    Attached Files Attached Files

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code to insert rows and lock command button

    Use my suggestion and replace 'clearcontents' by 'clear'.

  14. #14
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Thanks snb. I have added the code to my other part of the code that I use and it does not work. Here is the code

    Please Login or Register  to view this content.
    Any ideas?

  15. #15
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Hi Mike/snb any suggestions on this please? Thanks

  16. #16
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code to insert rows and lock command button

    Remove the code you added; only keep mine.
    For that purpose validation seems better suited.

  17. #17
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    Hi,

    If you can get by with not inserting a row as soon as something is typed then this will work.

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Thanks Mike. When a new line is inserted the borders (lines above and beneath totals) are inserted in each new row from column A. Also the macro button exapands each time a row is inserted. I need to lock the macro button so it's fixed in one place even if new rows are inserted or deleted.

    If a compromise needs to be made between locking the macro button and inserting new rows I'd rather be able to lock the macro button in place so when new rows are inserted manually the macro button does not change.

    Thanks

  19. #19
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    Hi,

    I suppose the reason your button is not working is because of setup

    if you right click your button
    Goto Properties
    Click the Dont Move or Size with cells

    It will solve your button issue moving.

    For some reason you dont have that option just delete the text in the button then try the steps above, then add your text back in.

    Here is an example workbook. Everything seems to be working on this test book?

    Let me know if you need more help.

    ..
    Attached Files Attached Files

  20. #20
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Thanks again Mike. I tried your file and have the same issue as I was having. The border line should only appear in columns D and E but as soon as a new row is inserted the border line appears in all the columns from column A. Thanks

  21. #21
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    Hi, lets see if we can work that out...

    This shouldl fix that issue..


    Thanks,
    Attached Files Attached Files
    Last edited by realniceguy5000; 06-29-2011 at 12:52 PM.

  22. #22
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Hi Mike...didn't work for me. I'm attaching a copy of the workbook with the required number of coulmns and the code. Thanks
    Attached Files Attached Files

  23. #23
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    That extra data makes a differance, Try This:
    Attached Files Attached Files

  24. #24
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Hi Mike...thanks so much. That works great. If you get time can you please explain to me how the code works so I know for future? Thanks

  25. #25
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Code to insert rows and lock command button

    Quote Originally Posted by fusion007 View Post
    Hi Mike...thanks so much. That works great. If you get time can you please explain to me how the code works so I know for future? Thanks
    Hi, Please don't forget to mark your post solved if this solved your issues,

    Here is a break down on what the script is doing...Just copy this script below and replace with the last script I sent you. I placed a stop command in the script so you can step through and watch what takes place at each line. You can press F8 to move from line to line.



    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    06-20-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    91

    Re: Code to insert rows and lock command button

    Thanks so much Mike. I will add reputation points for you. I will set this thread to solved.
    I have another thread open on calculations, I'm stuck on the last part. I hope you will be able to help! Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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