+ Reply to Thread
Results 1 to 4 of 4

Macro to Add and Delete Row (by unhiding and hiding)

  1. #1
    Registered User
    Join Date
    06-04-2013
    Location
    Bumble
    MS-Off Ver
    Excel 2003
    Posts
    14

    Macro to Add and Delete Row (by unhiding and hiding)

    Hi all

    I have a table with resource names and billable hours by week. I want to add the capability for users to add and delete rows. I want to do this by hiding and unhiding the rows the user can see. I have a flag indicator (column B in the sample file) that equals 1 if the row is visible and 0 if the row is hidden (I already have some code that will to do this part).

    What I need to do is add another couple of macros to:

    1. Add a new row by going through the values in B5:B15 to find the first 0 and change it to a 1.

    2. Delete a row by going through the values in B5:B15 to find the last 1 and changing it to a zero AND clearing the corresponding row cells in columns E to J (E7:J7 in this example).

    Can anyone help out with?

    Hugely appreciated.

    Thanks,
    Mo
    Attached Files Attached Files

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

    Re: Macro to Add and Delete Row (by unhiding and hiding)

    Hi there,

    The following code should do what you need:

    Please Login or Register  to view this content.

    The highlighted values may be changed to suit future workbook/worksheet layout changes.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    06-04-2013
    Location
    Bumble
    MS-Off Ver
    Excel 2003
    Posts
    14
    Hi Greg,

    This works great as long as it's in Sheet1. If I try to apply the code in my model where it's not in sheet 1 I get a "subscript out of range" error. I've tried changing the second line of the code (const msSHEET_NAME) to the correct sheet and I still get the error. Is there any other place i need to update the sheet number?

    Thanks a lot!
    Mo

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

    Re: Macro to Add and Delete Row (by unhiding and hiding)

    Hi again,

    There are two ways to approach this.

    The cheap 'n' cheerful (and risky!) way is to change the function as follows:

    Please Login or Register  to view this content.
    The risk is that you may accidentally run the application when an unintended worksheet happens to be active and you end up deleting important data.



    The more complicated (but safer) way is to define an array of worksheets on which you intend the application to operate:

    Please Login or Register  to view this content.
    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M

+ 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] macro for hiding and unhiding rows based on a dropdown selection and cell value
    By cameldew in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-17-2015, 03:52 PM
  2. hiding unhiding rows macro hierarchy
    By dgibney in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 11-19-2014, 01:44 PM
  3. Hiding/Unhiding Rows
    By marcheese in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2012, 11:56 PM
  4. Macro to increment hiding/unhiding of columns
    By gautamacharya in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-03-2011, 07:48 PM
  5. Hiding unhiding columns
    By ABSTRAKTUS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-17-2010, 11:24 AM
  6. Hiding/unhiding sheets
    By nkburm in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-27-2010, 03:49 PM
  7. Hiding/Unhiding
    By Bobby in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-25-2005, 05:43 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