+ Reply to Thread
Results 1 to 20 of 20

Clean up IF statement with multiple cell references

  1. #1
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Clean up IF statement with multiple cell references

    Hello,

    Anyone know of a tidier way of writing the below? It's some code I've got on a sheet module of a workbook which is being used to trigger a macro dependent on which cell is clicked. There's 12 in all for the months and im sure there's a shorter way of writing each month out without a new if statement each time.

    Please Login or Register  to view this content.
    I'm look for something along the lines of "if Jan (j7) clicked; change it's colour to XXXX and cells N7 + R7 + V7 + Z7 colour to XXXX etc...

    I feel else if would be good but cannot get it to work.

    Thanks

  2. #2
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Clean up IF statement with multiple cell references

    edit:

    got the EsleIf statement working.

    So just curious now if anyone knows a better way of writing:

    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,584

    Re: Clean up IF statement with multiple cell references

    You could do something like this for the January cell:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Clean up IF statement with multiple cell references

    Hi inq,

    See if this will work for you. This is the code for J7. I think you need a little For..Next loop with a Step of 4.
    Please Login or Register  to view this content.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Clean up IF statement with multiple cell references

    thanks guys.

    very interesting solutions i never even considered. I'm off to look at the other all code again to review.

    I'm basically setting up some buttons on screen without actually using buttons.

    The colour change is just to see which month has been selected, then load the appropriate data accordingly. I like how your code works MarvinP but cannot see how this would change them into selectable buttons; each one once clicked would need to change to the purple colour whilst the last one changes back to grey.

    a bit more playing around for me i think.

    thanks again guys though.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Clean up IF statement with multiple cell references

    Hi inq80,

    See the attached for another possible answer. If you double click on any cell in the first 20 rows (read the code and you can change this) it will clear all background colors and highlight the ones I think you want. Here is the workbook so you can see and read the code. Now that you've explained a bit more, I think this is what you really want.

  7. #7
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Clean up IF statement with multiple cell references

    Quote Originally Posted by MarvinP View Post
    Hi inq80,

    See the attached for another possible answer. If you double click on any cell in the first 20 rows (read the code and you can change this) it will clear all background colors and highlight the ones I think you want. Here is the workbook so you can see and read the code. Now that you've explained a bit more, I think this is what you really want.
    Thanks MarvinP

    It's good but not quite what I'm after. I've re-done sheet2 with what i have so far so you can see what i needed. I just need a better way of writing the line that changes all the non-selected cells back to grey; seems messy at the moment.

    Copy of Event Double Click to show cells to be added.xlsm
    Last edited by inq80; 08-02-2015 at 03:18 PM.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Clean up IF statement with multiple cell references

    OK here is another try.

    Double click on D5 on sheet2 and see what happens.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Clean up IF statement with multiple cell references

    that bounces around all over the place no matter which cell you click.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,584

    Re: Clean up IF statement with multiple cell references

    Did my solution not do what you were asking for? When you select a cell, it changes the colours as described. Easy enough to replicate for other months.

  11. #11
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Clean up IF statement with multiple cell references

    What you have on Sheet2 module right now, can be written like below also using the Select Case statements.
    Please Login or Register  to view this content.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  12. #12
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Clean up IF statement with multiple cell references

    @TMS

    He has merged cells so If Target.Cells.Count > 1 Then Exit Sub will not allow the code to proceed further.

  13. #13
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Clean up IF statement with multiple cell references

    Or even simply this, which seems very much identical...
    The previous code will produce an error if you select a single cell i.e. unmerged cell.

    Please Login or Register  to view this content.

  14. #14
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Clean up IF statement with multiple cell references

    OR simply this.......

    Please Login or Register  to view this content.
    Attached Files Attached Files

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,575

    Re: Clean up IF statement with multiple cell references

    try
    Please Login or Register  to view this content.
    Oh, I didn't see sktneer's.
    Last edited by jindon; 08-02-2015 at 05:46 PM.

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,584

    Re: Clean up IF statement with multiple cell references

    @sktneer: I did not know that when I asked the question. BUT ... you do need something or, if you select a whole row, or multiple cells, the effect is "interesting".

    I would suggest:

    Please Login or Register  to view this content.

    Regards, TMS

  17. #17
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Clean up IF statement with multiple cell references

    Quote Originally Posted by sktneer View Post
    OR simply this.......

    Please Login or Register  to view this content.
    That's beautiful!

    Far more elegant than mine

    I couldn't get the range thing to work for only selected cells in a row so ended up with my own code as posted. Of course it's far easier just to turn them all grey; then turn the target cell purple. Thanks sktneer.

    also thanks to everyone else for replying.

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,584

    Re: Clean up IF statement with multiple cell references

    Note post #16.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  19. #19
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Clean up IF statement with multiple cell references

    You're welcome. Glad it worked for you.

    Don't forget to include a line of code what TMS has suggested i.e.

    Please Login or Register  to view this content.
    Otherwise you will get unexpected result, so it would be better to exit sub if more than 3 cells are selected at once.

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,575

    Re: Clean up IF statement with multiple cell references

    Multiple cells could be selected, so should like this
    Please Login or Register  to view this content.

+ 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. Problem with IF statement and the cell it references
    By smythe6982 in forum Excel Formulas & Functions
    Replies: 21
    Last Post: 06-02-2015, 02:05 PM
  2. [SOLVED] Remove multiple Carriage Returns - macro to clean cell contents
    By Deskjocky in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-03-2014, 01:52 PM
  3. IF Statement Using Cell Ranges & References
    By RMH in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-27-2013, 03:40 AM
  4. [SOLVED] Is it possible to write an IF statement containing multiple references to 2 other cells?
    By Karelia Suite in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-31-2013, 08:11 AM
  5. Sending a Nested IF Equation with Sheet References and an OR Statement to a Cell
    By itclonghorn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-21-2013, 06:43 PM
  6. [SOLVED] VBA Code to optimize and clean data- clean out numerical/ or symbol
    By tracylsr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-05-2012, 04:21 PM
  7. Clean Function to Clean Entire Sheet
    By gema in forum Excel General
    Replies: 7
    Last Post: 11-05-2009, 10:07 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