+ Reply to Thread
Results 1 to 20 of 20

Toggle cell fill

  1. #1
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Toggle cell fill

    I have a relatively small range of data that I want to sort on two criteria using a command button. The criteria are cell fill and numeric value; sorting the filled cells first to the top (actually white cells to the bottom), and then sorting the two groups numerically individually. I was able sort the multiple columns of data with no problem using the macro recorder to obtain the code. I know it has lots of extra words and lines, but that's OK for now.

    However, what I thought would be the easy part, I can not get to work.

    I want to quickly toggle between white fill and a color fill in cells by simply clicking on a cell in the column used for the sort. (I did have all cells involved filled with either white or the color when tested.)

    There are actually two ranges on my worksheet that use different colors for sorting different data, and I don't want this color change to happen anywhere else on the worksheet, so I assumed it was best to define the range involved for each separate color.

    This is what I came up with:

    Please Login or Register  to view this content.
    What I get when I try to use it is this message:

    "Compile Error
    Invalid Use of property"

    and the "Private Sub" line turns yellow and the"Range" word is surrounded by black.

    And then the command button sorting code no longer works until I delete this code.


    Moderator Edit:

    Welcome to the forum.

    Please notice that code tags have been added to your post. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
    Thanks.
    Last edited by Cutter; 09-08-2012 at 02:32 PM. Reason: Added code tags

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    maybe like this:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  3. #3
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Thanks, but just looking at it, looks like it would turn a white fill to cyan, but not a cyan fill back to white?
    But I do see my many other syntax errors. Will try to modify the Else part.

  4. #4
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Never mind. I am confused, but it works as you did the code, without needing the second If/Then. I have lots to learn.
    Thanks again.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    as long as you want only 2 colours in a range it´s pretty easy: the cell has the given CI (ColorIndex) than switch, or if not, give that to the cell.

    Please Login or Register  to view this content.
    is just a definition of a range but Excel doesn´t know what to do with it. There must be any action (Value is the Standard for the range but that would need any value to be put in).

    The first End Sub will end the procedure, and that will cause the compiler to claim that there´s an If without an End If.

    I have lots to learn.
    You can put me on that list as well - I´m still learning about the Application Model of Excel2000, and a lot has changed in the later Versions.

    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Guten Nacht Holger,
    Your code worked great, except when I tried to copy it to work for my second range with a different color I get this error:
    Ambiguous name detected: Worksheet_SelectionChange.
    Hope you are still up tonight!

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    behind each sheet there may only be one event of the very same name. So here we try your Else-Case (please change the range address and colour as needed):

    Please Login or Register  to view this content.
    HTH
    Holger

  8. #8
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Amazing!

    I am learning more here than the 475 of 1020 pages I have read in Walkenbachs "Power Programming With VBA"
    I don't want to become a burden. But here is one more challenge:

    If I wanted to make sure my double sort (color and number) could not be done ( pressing the command button would open a message box with text "Must select five", for example) unless 5, and only 5 cells had been filled with color, what would you add to the code?

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    I´d maybe use a different way to a MsgBox with this quick and dirty version

    Please Login or Register  to view this content.
    You could use the Else for a MsgBox to the user about not having chosen 5 cells.

    Ciao,
    Holger
    Last edited by HaHoBe; 09-08-2012 at 04:20 PM. Reason: typos - lots of...

  10. #10
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Amazing!

    I am learning more here than the 475 of 1020 pages I have read in Walkenbachs "Power Programming With VBA"
    I don't want to become a burden. But here is one more challenge:

    If I wanted to make sure my double sort (color and number) could not be done ( pressing the command button would open a message box with text "Must select five", for example) unless 5, and only 5 cells had been filled with color, what would you add to the code?

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    duplicate post to #8 - I think I need not repost #9, do I? Or did you already test it (with my not that good knowledge of the english language while creating some "new" words)?

    Ciao,
    Holger

  12. #12
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    So this would be inserted after the "Private Sub . . . " declaration?

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    if you want it there then yes. But I restricted the selection in that event to only 1 cell because otherwise you have to check for the areas of the selection and the number of cells therein and according to my knowledge loop through every single cell of the selection. That would mean a different code than the one I posted, and I need to know how this code shall be started: if 5 cells within the given ranges are selected? Or if any cell is within any of the given ranges?

    Ciao,
    Holger

  14. #14
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Did I not send the message correctly? Not sure what happened.

    Again - In the beginning there will be no cells with color. Then I will pick five. If I do not pick five, the program should tell me to pick five. Then I might choose to change 1,2,3,4, or 5 of the cells from color to white, and the same number from white to color. The program should tell me if I try to sort without five colored cells selected in the range.
    Last edited by ballan; 09-08-2012 at 05:32 PM.

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, balan,

    from what I read from your last post I think the SelectionChange-Event might not be the best solution available. I prefer the Worksheet_BeforeDoubleClick or Worksheet_BeforeRightClick and suppress their default behaviour by using Cancel = True in the code.

    If I understand the problem correctly you want to select any given number of cells and want to change the colour for those cells. There are 2 ranges where this action has to be allowed. If the colouring has taken place there should be a count to find out if there are 5 cells filled with the colour and then sort the range (could you supply the code for that, please - I know how to sort in 2010 but I need your ranges and criteria).

    I would probably use a tab on the ribbon to start the action from instead of using an event driven approach.

    Ciao,
    Holger

  16. #16
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Hi Holger,

    I am up at 4AM here. I have been thinking about this all night. Your code for changing the cell color is perfect. I am very grateful for your help.

    It is the code for the sort command button that needs to count the number of cells in the range that have been filled with color. There are two command buttons - one for each color. If that number is not five when the sort command button is clicked, then the sort should not be done and a message box should appear with text = "Must select five." So I assume I need to add code to do this count at the beginning of the sort code for the command button. And if the count does not equal five, the subroutine ends and displays the message box. This is just an error handling routine. Most of the time, the person should remember to pick five items without help!

    I will send the sort command button code later. I need to figure out the correct way to do that. I was warned by the moderator that I need to add tags to my code?

  17. #17
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Here is the sort code I created for one of the two command buttons when I used the macro recorder.
    I realize it is probably much longer than required.

    Please Login or Register  to view this content.

  18. #18
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, ballan,

    I am up at 4AM here
    Such a big item with the code?

    I´ll be out for approximately 2 hours from now on and look at the code when I´m back (FYI: it´s 10:45 AM local time for me right now). Most hopefully I may supply a guess at how to solve this issue.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  19. #19
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Toggle cell fill

    Hi, balan,

    basically I left the code alone and just worked a bit on the coding. As you use it for two buttons you will have two nearly identical procedures which will only differ in the ranges. You may think about using two more procedures which could be called from the buttons with the parameters of the range to sort passed on.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  20. #20
    Registered User
    Join Date
    09-08-2012
    Location
    RTP, NC
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Toggle cell fill

    Thanks for all your help on this Holger. This latest change does not seem to work. I installed the code for HSORT button and it no longer does anything. No message box no matter the number of cells filled, and no sorting. But I am happy with the function without forcing the need to have only 5 cells filled. Thanks again.

+ 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