+ Reply to Thread
Results 1 to 15 of 15

Add New Row by clicking a Button using VBA

  1. #1
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Add New Row by clicking a Button using VBA

    Hi Everyone,

    I have the below code to insert new rows in my worksheet. This code is working fine but I need some modifications in this. I need rows to be inserted not before the last row in sheet but when it finds "Last Row" in column C, it should insert row above it. Below this "Last Row", I do have some formulas that calculates the total value from the above rows.
    This code currently finds the last row in sheet and adds a new row and copy down the formulas from cell C18:DU18 which is correct.
    Can you help me to modify this code so that it only adds row above "Last Row" and still copies down formula from C18:DU18

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    Also I just realised that this VBA is also copying the contents along with the formula.. Can the VBA just copy the formula and not contents.

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    Perhaps this:

    Please Login or Register  to view this content.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    Hi Alan,

    The code u provided is failing on the below line:
    Range("C" & lr).PasteSpecial xlPasteFormulas
    I guess something wrong here which I cant figure out as I am new to VBA thing.
    Also I need this code to insert a new row above when it finds "Last Row" in column C. This code just enters a new row at the last line which i DONT need. is this also achievable in this code.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    try this. Make changes to suit your particular worksheet as noted.

    Please Login or Register  to view this content.
    Last edited by alansidman; 05-29-2018 at 08:58 PM.

  6. #6
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    This code is working but again its copying the contents as well along with the formula's in the new rows.
    Also this code is adding row above the last row but I want this code to find "Last Row" text in column C and add new rows above it. I have typed "Last Row" in the last row in column C so that any new rows can be inserted above it. Is it possible?

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    Since we are going around on this, it is time to provide a sample work book showing a before and after scenario. You will need to mock up manually the after scenario.

    If you have confidential data in your workbook, dummy it up.

  8. #8
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    I have attached a dummy file with before and after. I need rows to be added above Last Row text string in column C. And the new rows should copy the formulas only from column C18 to DU18 in my sheet. The row after the text string Last Row has got totals from the above.
    Attached Files Attached Files

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    This should take care of the insertion correctly. My apologies in understanding what your last row was. Since there is no data in your cells to copy, I am not sure what is happening there. Do you have calculations set to manual?

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    Now its adding row where i wanted but again its just copying the data from the above rows C18 to DU18. Its not copying formulas down in each row. I have formulas in each row as drop down or as look up function in some rows.

  11. #11
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    I am not understanding what is the issue with the copying. I suspect it has to do with the comboboxes as you indicated above. Since you have not shown what is in Row 18 or its layout, there is nothing I can offer you. You are asking me to solve a problem I cannot see. It is much like being turned around a couple of times blindfolded and then asked to hit the bulls eye throwing darts. Time for bed. I will check back tomorrow.

  12. #12
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    okay I just added one line in your code and its working partially for me. I just added the below line in the code:
    Range("C" & lr).ClearContents
    below is complete code.
    Its now clearing contents from column C and retaining formula and formatting but rest of the columns still have the contents and formula both. How can this command be extended in the code to clear contents of the new added row in all the columns until DU and retain the formula and formatting and NOT contents.

    Please Login or Register  to view this content.
    Last edited by pchugh; 05-29-2018 at 11:22 PM.

  13. #13
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    If I am understanding your request correctly, which I am not sure I do, then the following code should do what you are asking.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    05-04-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2016
    Posts
    65

    Re: Add New Row by clicking a Button using VBA

    Thank you Alan. I guess I have achieved what I was intending to. Thank you again.

  15. #15
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Add New Row by clicking a Button using VBA

    you are welcome and thanks for the rep

+ 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. Clicking a button on Web using VBA
    By sourabhg98 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-14-2016, 11:21 AM
  2. VBA button to copy values from one range to another by clicking the button
    By pedrostur in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-07-2016, 03:49 PM
  3. #How Clicking the button with Id and Name
    By magushi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-19-2014, 11:55 AM
  4. clicking website button
    By Megatronixs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-09-2014, 07:28 AM
  5. VBA/IE button clicking issue
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-24-2013, 03:29 PM
  6. [SOLVED] clicking a button on a promt box VBA
    By mike02 in forum Excel General
    Replies: 5
    Last Post: 08-03-2012, 11:59 AM
  7. [SOLVED] How do I run the macro for a button click without clicking the button?
    By flyboy54 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2012, 10:45 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