+ Reply to Thread
Results 1 to 9 of 9

VBA + SelectionChange event

  1. #1
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Arrow VBA + SelectionChange event

    Hello all....

    I'm sure I need to use some sort of OFFSET in my VBA code but I can't find an example of what I need.

    Here is what I am trying to do:

    Suppose I have a Named Range..."TrueFalse" = (Sheet2!A1:A10,Sheet2!E1:E10,Sheet2!I1:I10)

    Each cell in that Named Range can be either TRUE or FALSE.

    If TRUE...I want the 3 cells to the right of each cell to be merged.
    If FALSE...I want the 3 cells to the right of each cell to be UNmerged.

    As the value in each cell is changed, I want the MERGE to change as well.

    I think I am on the right track placing this into a SelectionChange Sub in Worksheet 2....
    I just don't know how to construct the code. (Or am I way off base??)

    Any suggestions...???
    Last edited by HuskerBronco; 02-13-2011 at 02:15 PM. Reason: mark solved

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: VBA + SelectionChange event

    Why do the cells need to be merged?

  3. #3
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: VBA + SelectionChange event

    The spreadsheet is for a TV schedule.

    Say, for example...

    Cell B1 would belong to a 1/2 hour time slot (from 7 PM to 7:30 PM)
    Cell D1 would belong to the 7:30 PM to 8:00 pm time slot.

    TRUE in Cell A1 would indicate an HOUR long program.
    So, I want Cells B1:D1 to merge, and the program title to be centered across the selection.

    FALSE in Cell A1 would UNmerge the cells.

    Then the schedule will look less cluttered when printed out.
    Last edited by HuskerBronco; 02-13-2011 at 08:48 AM. Reason: Added to comment

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: VBA + SelectionChange event

    Try this
    Please Login or Register  to view this content.

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

    Re: VBA + SelectionChange event

    Please Login or Register  to view this content.



  6. #6
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Thumbs up Re: VBA + SelectionChange event

    Thank you, very much...guys!!! (davesexcel & snb)

    You both replied with nearly identical code.

    snb...I still haven't gotten the RESIZE function into my VBA mindset.
    I've used the function before, but never think about it when writing new code.

    davesexcel...While searching the internet for this code, I ran into a great deal of
    conversation about the EVIL of merging cells in Excel spreadsheets.
    I seems that the general consensus is that you should always try to AVOID this practice.
    Can you give me some insight as to why this is considered "taboo".

    Thanks, again...to both of you, for your help.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA + SelectionChange event

    why this is considered "taboo".
    In most cases, merged cells require special handling in VBA.

    In the general case, you can’t sort a table that contains merged cells.

    Lookup (and many other) functions don’t work if the referenced ranges contain merged cells.

    Merged cells should be reserved for presentation, and should be output-only; i.e., they should not be referenced by other formulas, and not be used (except perhaps to be written) by VBA.
    Entia non sunt multiplicanda sine necessitate

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

    Re: VBA + SelectionChange event

    Generally speaking the practical feature of VBA is you can refer directly to a cell, column or row to read/write/manipulate.
    With merged cells VBA gets confused (which cell, which row, which column). The last thing you want to be confronted with in programming is confusion: digital/binary systems are 'confusionfobic'.
    Last edited by snb; 02-14-2011 at 04:42 AM.

  9. #9
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: VBA + SelectionChange event

    Quote Originally Posted by shg View Post
    In the general case, you can’t sort a table that contains merged cells.

    Lookup (and many other) functions don’t work if the referenced ranges contain merged cells.
    Now I understand.

    I've had numerous instances in the past where merged cells have "broken"
    or "crippled" my formulas.

    Never realized, until now, that I was causing myself so much grief.

+ 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