+ Reply to Thread
Results 1 to 5 of 5

How to insert rows depending on value of a reference cell?

  1. #1
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Lightbulb How to insert rows depending on value of a reference cell?

    Hi Everyone!

    I'm looking for a fix (possibly VBA code, or whatever else will work) that will DELETE rows in multiple sheets in multiple areas depending on the value of a reference cell.

    My spreadsheet is set up so that someone enters some information regarding a shipment of goods, and then that information is transferred to multiple different sheets to create shipping documents. These documents are then automatically saved as PDF files and sent to myself in an email. The issue is that, depending on the number of "Goods" I'm shipping (the maximum being 6), I would like to have rows in these shipping documents automatically deleted to shorten the length of the document and not create 'blank space' in them. I'm electing for deleting rows (rather than adding) since of course these rows have formulas to carry over the data from the "Input" tab.

    Example: If, in the Input tab, it's noted that there are 4 goods being shipped, perhaps I click a button to run the macro/VBA code, which then deletes the unnecessary rows in each of these shipping documents. I've added some photos to make things more clear:

    Let's say I have chosen 3 x goods to be shipped. After running the code, it would go into "Document #1" (PHOTO#1) and delete rows 25, 26, & 27, leaving only rows 22, 23, and 24 with the data of the goods being shipped. It would also go into "Document #2" (PHOTO #2) and delete rows 22:24, 30:32, and 38:40. The row placement will always be the same in each of these documents.

    I've also attached the workbook itself if that makes it easier to understand. Will greatly appreciate anybody's help with this, and if this turns into being a lot of work I'm happy to pay for it.. $10, $20 or whatever you guys/gals think this is worth. I have gotten a lot of help from this site and I am beginning to feel bad as I have not been able to offer much in return to date. I am doing my very best to learn how to code but it is hard!!
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,055

    Re: How to insert rows depending on value of a reference cell?

    Does it work if you hide the rows?

    I created named ranges for all of the hideable areas.
    In a module I added a sub to hide the rows based on the input sheet.

    A change even in the input sheet runs the sub in the module.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    05-30-2016
    Location
    CZ
    MS-Off Ver
    O365
    Posts
    19

    Re: How to insert rows depending on value of a reference cell?

    Hi, try this - it can add or remove a number of rows that you enter into the B3 cell with the click of a button. Only one thing - the number in cell B1 (updates with each run) should not be deleted.

    Insert Rows VBS.xlsm

  4. #4
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Re: How to insert rows depending on value of a reference cell?

    Quote Originally Posted by ByteMarks View Post
    Does it work if you hide the rows?

    I created named ranges for all of the hideable areas.
    In a module I added a sub to hide the rows based on the input sheet.

    A change even in the input sheet runs the sub in the module.
    Wow this works seamlessly in that spreadsheet! So I tried moving this code over to my active spreadsheet where there are of course more documents. I'm getting an error whenever I change the value of "Good to be shipped" in my active spreadsheet. It should be noted that I'm entering the data on the "Input" tab and the # of Goods cell value is in cell I26, so I changed that range in the code as well.

    The error I'm getting is: "Method 'Range' of object'_Global' failed

    Which points to this area in the code:
    Please Login or Register  to view this content.

    This is what I changed the private sub code to:
    Please Login or Register  to view this content.
    Can you see what has gone wrong here by chance?

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,055

    Re: How to insert rows depending on value of a reference cell?

    Use the Name Manager (Ctrl+F3) to check that the named ranges exist and are scoped at workbook level.

+ 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] VBA to Insert Formatted Rows Based On Cell Reference
    By hindotmo12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-09-2020, 06:03 PM
  2. [SOLVED] VBA / copy & insert rows with specific columns depending on cell value
    By thedefense in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2017, 11:09 AM
  3. Insert new rows, keep cell reference in another worksheet. INDIRECT does not work
    By dazedandconfusedd in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 01-18-2016, 09:39 PM
  4. [SOLVED] Insert multiple rows depending on cell value
    By AR-51 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2013, 04:18 AM
  5. Replies: 14
    Last Post: 11-14-2012, 11:24 AM
  6. macro: insert specified number of rows depending on referenced cell value
    By cffurillo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2012, 11:37 AM
  7. Auto insert 'x' rows depending on value 'y'
    By nephilim3uk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-13-2010, 09:49 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