+ Reply to Thread
Results 1 to 12 of 12

Expand a range when you click any cell in the last line of the range

  1. #1
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Expand a range when you click any cell in the last line of the range

    I would like the data entry portion of my worksheet to expand automatically when you try to make an entry on the last available line.
    The attached spreadsheet is a portion of a workbook used to calculate how much concrete I need to do a job.

    I have the following named ranges:
    SlabData=E6:P9 (This named range may not be needed)
    SlabDataLastRow=E9:P9
    Note that shaded cells contain formulas. Everything in the last row needs to be copied into the new rows.
    Note that there are merged cells that need to be copied.


    Clicking in any cell in "SlabDataLastRow" should do the following:
    1. Insert three full rows (not cells) above row 9 - (I would like to be able to edit the VBA code to change this from 3 to any value such as 1,2,4 etc.)
    2. Copy the original entire row 9 and paste it into all of the new rows.
    3. Activate the cell that was clicked in the first place. From the user's perspective three new rows will magically appear below the active cell.
    4. The Name "SlabDataLastRow" will now refer to E12:P12 and SlabData=E6:P12


    Thanks in advance,

    Rob
    Attached Files Attached Files
    Last edited by Dickie Dunkin; 04-06-2016 at 01:40 PM. Reason: Forgot Attachment

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,938

    Re: Expand a range when you click any cell in the last line of the range

    USe this event code in your worksheet's codemodule

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Expand a range when you click any cell in the last line of the range

    Hi Rob

    Try this...
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    Bernie - thank you for the quick reply!

    There's just one small hiccup. The cell E9 is actually a merged cell composed of E9 and F9. For whatever reason, clicking in E9 does not trigger the code. Clicking in any cell G9:P9 works exactly as desired.

    I was hoping to duplicate the code several times on the same worksheet. I am going to create other named ranges such as "PitDataLastRow", "MaterialsDataLastRow","RentalDataLastRow" etc. and thought I would be able to duplicate the code for each named range, but it doesn't work that way.

    When I duplicate the code with "PitDataLastRow" I get "Compile error: Ambiguous name detected: Worksheet_SelectionChange".

    I now see that a worksheet can have only one "selection change".

    Please see if you can still help me in light of this new requirement.

    Thanks again,

    Rob

  5. #5
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    jaslake - thanks to you too for the quick reply!

    Your code works great! I double-click in any cell in the range (even merged cells) and three new rows appear. I know how to change the code from a double-click to a single click, but then I have the same problem I described to Bernie in my previous reply.

    As you may see from my last reply, I'm trying to make multiple named ranges auto-expand.

    When I duplicate the code with "PitDataLastRow" I get "Compile error: Ambiguous name detected: Worksheet_BeforeDoubleClick".

    Please see if you can come up with a method of handling multiple named ranges on the same worksheet.

    Thanks again,

    Rob

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,938

    Re: Expand a range when you click any cell in the last line of the range

    The failure with merged cells is because of the multi-cell check that I use, so I took that out.

    For the multiple areas, try the code like this: I hope you see the pattern to be able to expand it.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    Thanks! That's exactly what I wanted and I'm sure I can duplicate it as needed.

    I really learned something here - you just nested all of the code under a single "Worksheet_SelectionChange" event - no need for each range to have its' own event.

    You also fixed it so that clicking in a merged cell triggers the code.

    Thanks,

    Rob

  8. #8
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    Bernie - I've hit a snag! Please see the newly uploaded spreadsheet.

    Each row contains several formulas that duplicate the value in the cell above.

    For example: In the range "SlabDataLastRow" The formula in H12 is =H11
    After the code runs the formulas are not correct. With code set to insert 3 rows, H12 becomes H15, but the formula in H15 still reads =H11 when it should read =H14. When this incorrect row is copied to the three rows above, those new formulas are incorrect as well. The "new" H12 reads =H8 when it should read =H11.

    The present code copies the row that contains "SlabDataLastRow" and then inserts three rows above.
    I think what it needs to do is insert the three rows FIRST (this will make the equations change) and then copy the last row into the three blank rows.

    Thanks in advance!

    Rob
    Attached Files Attached Files
    Last edited by Dickie Dunkin; 04-17-2016 at 11:53 AM. Reason: New Information

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,938

    Re: Expand a range when you click any cell in the last line of the range

    Try changing each instance of

    .EntireRow.Copy

    .EntireRow.Resize(iRowCount).Insert


    to

    .EntireRow.Resize(iRowCount).Insert

    .EntireRow(-iRowCount).Copy .EntireRow(-(iRowCount - 1)).Resize(iRowCount + 1)

  10. #10
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    That worked! Thanks again!

  11. #11
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    One last glitch....

    Inserting rows works as designed and Row 12 is now row 15 since I'm adding 3 rows at a time.
    Now, if you want to UNDO adding the rows, select rows 12,13,14 and delete the entire rows.
    Cell H12 now reads #REF!
    I think this is due to the fact that rows 12,13,14 are still selected, and the selection now includes the range "SlabDataLastRow"

    Thanks,

    Rob

  12. #12
    Registered User
    Join Date
    03-03-2016
    Location
    Anaheim, CA
    MS-Off Ver
    2003
    Posts
    28

    Re: Expand a range when you click any cell in the last line of the range

    More information - the problem is not what I thought it was.
    The true formula in H12 is =IF(OR($O12>0,G12>0),H11,"") which normally "boils down" to =H11.

    However, if I delete any row, say row 8, H8 will now read =IF(OR($O8>0,G8>0),#REF!,"") instead of =IF(OR($O8>0,G8>0),H7,"")

    Please advise.

    Rob

+ 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] expand the range of a named range
    By ammartino44 in forum Excel General
    Replies: 3
    Last Post: 04-10-2015, 01:26 PM
  2. I need to select a range on a cell click
    By countingbackwards in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 10-28-2013, 04:02 PM
  3. Auto Expand Cell - Double Click
    By curtjer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2012, 01:19 PM
  4. Auto expand range based on first cell selection
    By Jimmy Moggles in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2011, 07:56 AM
  5. Concatenate a range rather than click each cell
    By Spac3Monkey in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-09-2009, 06:18 AM
  6. Replies: 1
    Last Post: 07-07-2005, 09:05 AM
  7. click on a cell to expand with more information
    By Dodge Lisa in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 05-06-2005, 05:06 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