+ Reply to Thread
Results 1 to 21 of 21

fields as multiple switches

  1. #1
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    fields as multiple switches

    Given: an array with (more than) 3 columns. One is an on/off switch. Another is a unique identifier. The third is a list of other unique identifiers "linked" to that row/entry. When a given entry is turned "on",all the items in it's list are also turned on, and vice versa. I'm attaching a simple example, though in the real world the identifiers are code numbers, so they are things like 603.4 and 709.4.2.1.3

    Any thoughts on an easyish way to do this? I think of using a macro associated with cell/worksheet changes, but then parsing the list and searching seems likely to be slow. Thanks for any help.

    Keith
    Attached Files Attached Files
    Last edited by keithwins; 12-13-2014 at 02:11 AM. Reason: oops, forgot attachment

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: fields as multiple switches

    Im really not sure what your question is here? What are you trying to do, and what would a sample answer look like?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    It's a little convoluted, but I'd hoped the attached file sort of illustrated it. When I click/select "On" in a row, then all rows identified in it's "related rows" field (I didn't name it in the file) should also automatically turn/click/select to "On". And vice versa. There won't be any self-referential loops in there, so that's not a problem, though there are some other troublesome unresolved considerations. But first I need to figure out if it can be done efficiently enough to take this approach.

    K

  4. #4
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    When I said "and vice versa", I meant "when I select "Off" in a row, then all rows identified in it's "related rows" field should automatically be switched to "Off". I realized that was not a clear statement.

    K

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    Can I try to interpret your requirement:

    From the attached file, let's look at row 1.

    Cell C1 contains "b, c, f"
    If you change A1 to "off", do you want the rows which contain "b", "c" or "f" in column B to also show "off" in column A?

    If this IS the case, you'll need some VBA.

    Can you confirm this is what you want? If not, can you clarify?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: fields as multiple switches

    Thanks for the assist her, Olly

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    If my assumption is correct, then maybe try this, in the worksheet module:
    Please Login or Register  to view this content.
    *Disclaimer: It is 1:23am, I've been drinking wine for over 6 hours...*
    Last edited by Olly; 12-13-2014 at 09:25 PM.

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    Here's a revised file, showing my assumption and solution.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    First, sorry I was offline for a while. And thanks for your interpretation, I think you have it right. I'm going to digest your code a little. Perhaps you can answer a few questions?

    I assume turning off ScreenUpdating & EnableEvents just makes Worksheet_Change better? Any other reason there?

    I don't understand the Target.cells.count>1 thing: I guess that keeps it from trying to run the For loop if you replace/fill multiple cells simultaneously?

    I still need to digest your For loops, but that's because I'm still getting up to speed on VBA. Thanks a TON for your help, this is really great. I tried your code and it works perfectly. Thanks again.

  10. #10
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    Also: if I replace the a, b, c in the example file with the actual contents that are in the real file: things like 1.4.2.1, 609.3, etc, it no longer works (actually, it only works on the first entry). I think that's a LBound/UBound problem, but I can't quite tell why you use them yet...

    K

  11. #11
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    Try changing this line, so that it will work with numeric values as well as text:
    Please Login or Register  to view this content.
    To answer your questions:
    Disabling Screenupdating improves performance and prevents screen flicker.
    Disabling Events prevents the code being called recursively whenever changes are made to the worksheet by the code.
    The target.cells.count code does exactly as you say, and means the code only runs if just one cell is being changed.

    Hope that helps.

  12. #12
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    Olly, that seems to work great. Thanks so much. Very Very helpful.

  13. #13
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    If you (Olly) or anyone feels like educating:

    Why the LBound? I think I understand all the code now, but it seems to me that if an array (x in the code) has just been created by the split function, it will always have a lower bound/index of 0. Why not just run the For loop starting at 0?

    Thanks again, this is great. Huge.

  14. #14
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    Looping through arrays from Lbound to Ubound is just good practice.

    If unspecified, array indices will start at 0, but sometimes it's helpful to specify they start at 1 (or even something else again) instead. If we make an assumption about the lower bound index of an array, it may introduce an error if that is nor as expected. So good practice is to loop from Lbound to Ubound, knowing that we will then make sure we include the full array range

    In this case, while you are correct that the split function creates a lower bound of 0, the approach of using lbound means that if we were to change the method of creating our array, we wouldn't have to worry about whether that lower bound value had changed - we know the loop will continue to work.

  15. #15
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    Excellent, I rather figured that was it. Thanks!

  16. #16
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    Hey Olly: I tried to begin to incorporate your code into my existing worksheet_change, and broke it (new thread in VBA forum). But in the meantime, I think I discerned that I'd want to change the

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    to avoid leaving ScreenUpdating and EnableEvents False. But I think my playing with this might have broken something, and I'm not figuring out how to get it working again.
    Last edited by keithwins; 12-15-2014 at 01:18 PM. Reason: add => change

  17. #17
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    Ooops. My mistake - I coded that poorly. I did warn it was rather late at night!!

    Change the initial section to:
    Please Login or Register  to view this content.
    Then is more than one cell is changed the routine will exit before it changes the application settings.

    Your problem may now be that application events remain disabled, so worksheet changes are no longer triggering the code. To correct the problem now, open the immediates window in the VBE (Ctrl-G), and enter the following:
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    I suppose the other thing I could do is put the Target.Cells.Count test above where I turn off ScreenUpdating and EnableEvents, and then I wouldn't have to worry about turning them back on in case I pop out of the sub

  19. #19
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: fields as multiple switches

    Erm, that's what I just suggested...!

  20. #20
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    Also, as I noted in the other thread, I'd simply coded my Case statement wrong when I converted an If to a Case. I think I might have all the code pieces in place now, thanks so much for your help Olly. I've got to sort out a little formatting/splitting thing, probably involving regular expressions or something... there may be another thread on the way! Anyway, this has been incredibly helpful, and the syntax of VBA is not looking as inscrutible (or ugly -- no offense) as it was at first. There are some curious choices in there though... Speaking of which, is Worksheet_Change just a (reserved?) word, or does the underscore mean something (like the period) in VBA? I've been curious about that...

    Keith

  21. #21
    Registered User
    Join Date
    04-01-2009
    Location
    WashDC
    MS-Off Ver
    Excel 2003
    Posts
    84

    Re: fields as multiple switches

    Quote Originally Posted by Olly View Post
    Erm, that's what I just suggested...!
    Yes, sorry, our threads crossed, or more precisely I probably didn't update before sending a slowly-composed frequently-distracted response.

+ 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. Replies: 5
    Last Post: 11-21-2014, 10:00 PM
  2. [SOLVED] Using Drop downs to act as switches
    By steveh1873 in forum Excel General
    Replies: 6
    Last Post: 01-06-2014, 11:19 AM
  3. formula switches to value ?!?
    By mark kubicki in forum Excel General
    Replies: 4
    Last Post: 05-11-2006, 03:15 PM
  4. Creating switches
    By kevatt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2006, 01:00 AM
  5. [SOLVED] Macros with switches
    By vishu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2005, 07:05 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