+ Reply to Thread
Results 1 to 32 of 32

VBA working with conditional formatting

  1. #1
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    VBA working with conditional formatting

    I have this code working in an .xls file but it won't work in another whether it is .xls or .xlsm
    I have another formula in conditional formatting working with this macro to highlight the row/column of the active cell.
    I can do an F9 recalculate to make the formatting work, can't figure out how to make it auto calculate
    like it does in the other spreadsheet.

    Also, what is the signifance of the General vs. workbook and worksheet_selectionchange vs. (declarations) and open

    Thanks,

    Private Sub Workbook_Open()

    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.ScreenUpdating = True
    End Sub

  2. #2
    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,447

    Re: VBA working with conditional formatting

    Please post a sample workbook.

    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


  3. #3
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    macro won't workDAILY REFERENCE SHEET.xlsm
    macro worksExpress Chipping Invoice Reconciliation.xls

    I recently moved from XP to Windows 7 for background

  4. #4
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2016
    Posts
    957

    Re: VBA working with conditional formatting

    Is this what you were trying to accomplish ref your conditional formatting of column & rows? See revised sample xlsm file. Otherwise reference your totals... there are no formulas to calculate or sum anything in your "total" column. Same thing with cell "B3" there isn't a formula to add anything. Sorry If I'm missing something.
    Attached Files Attached Files
    Last edited by lilsnoop; 12-30-2011 at 11:14 PM.

  5. #5
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    When I opened your version it worked as it should.
    I then saved it and reopened it and it won't work until I press F9 to recalculate manually.
    What happens in the save/reopen that will disable the automatic recalculate that worked previously.
    What in the VBA performs the F9 function automatically is another way of asking the question I guess.
    Thanks

  6. #6
    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,447

    Re: VBA working with conditional formatting

    Excel, for some reason, keeps a memory of the calculation mode/status.

    When you open the first workbook in a session, it uses the calculation mode set in that workbook and applies it to every other workbook. So, if the first workbook you open has the calculation mode set to manual, that's what it will use ... and it will retain the setting for the next Excel session.

    Try setting the calculation mode to automatic before you save and close the workbook. And see what happens when you start another Excel session.

    Regards, TMS

  7. #7
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Checked that first thing, set for automatic under Excel Options

  8. #8
    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,447

    Re: VBA working with conditional formatting

    Maybe you need to check it after opening each workbook.

    Regards

  9. #9
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    No Joy, the macro only updates manually for the formatting.
    The only thing I changed is a new PC, moved from XP to Windows 7.

  10. #10
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Perhaps if I restate my problem. The VBA coding used to work fine on my XP system. The coding worked the first time I opened your file, once saved on the Windows 7 system it will not work when opened again. If I open it from your link in this thread the coding no longer works as it did the first time.
    All instances of formula recalculation is set to automatic.
    The conditional formatting changes when F9 is pressed in all instances.

    Two questions, what in the VBA recalculates the worksheet to invoke the conditional formatting when the cell location is changed; and, what is so different in Windows 7 interacting with Excel 2007 than XP to make this glitch happen and how to fix it?

  11. #11
    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,447

    Re: VBA working with conditional formatting

    OK, I have to admit to being very confused ... now I have looked at your examples.

    The .xls has code in it ... worksheet change events. The .xlsm file did not have any code in it and lilsnoop copied the code into one of the sheets.

    As far as I can see, when you change the contents of cell H3, the code (when it is in place) searches for the first occurrence of the value. The CF is base on the address of the cell that is found.

    I must be missing something ...


    Regards, TMS

  12. #12
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Which worksheet of which workbook are you referring to the H3 cell?

  13. #13
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2016
    Posts
    957

    Re: VBA working with conditional formatting

    Markstro-In the workbook you said worked; specifically the conditional formatting part, it had the following vba coding on the specific sheet you said worked: (This is the same coding I placed on the sheet you said wasn't working)
    Please Login or Register  to view this content.
    It is here where the cell "H3" is referenced. I hope that clarifies your question.

  14. #14
    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,447

    Re: VBA working with conditional formatting

    In the .xls file (the one that works), you had the following code in sheets "2010" and "2011"

    Please Login or Register  to view this content.

    In the .xlsm workbook (the one that doesn't work) there is no code in any of the sheets, only the following code in the workbook module:

    Please Login or Register  to view this content.

    Maybe you have uploaded the wrong example?


    As lilsnoop says, the code, together with the CF, causes the cells to change colour.


    I'm not entirely sure what it is you are trying to achieve as the cell H3 doesn't seem particularly relevant.


    Regards, TMS

  15. #15
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    The code with H3 in it is not related to my problem, unless it may be causing the problem.
    I am thinking the platform from XP to W7 may have a bearing on this. Anything on that line of thought?????

    The worksheet_selectionChange code is the one I can't figure out.
    This is an integral part of how I navigate my spreadsheets and others who use them as well.

    The H3 code is for searching the sheet without hitting Ctrl F to find a particular truck number I am searching for, I may need to unbug that as well.

  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,447

    Re: VBA working with conditional formatting

    Not sure what you mean by "integral part of how you navigate your sheets".

    It doesn't really do anything. Generally, you switch OFF screen updating to speed up processing while a macro runs and then switch it ON again when you want/need to refresh the screen. As I can't see anywhere that you switch off screen updating, it is redundant.

    I've tried applying the CF formula to empty sheets in Excel 2003 and 2007; results attached. Not sure they operate in exactly the same way but that's no real surprise.

    I tend to agree that the code has little or nothing to do with the problem unless 2003 and 2007 aren't picking up the selection change in quite the same way.

    Afraid I'm all out of ideas.

    Regards, TMS
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Because of the large spreadsheets we use in my company, I found out how to highlight the column & row, then make it constantly update as you moved the cursors.
    It worked like a charm for everyone who need to enter data or view it, makes ensuring you're looking at the correct header row and column is all.
    This problem is very similar to your test sheets in the previous message, I discovered the same behavior early on when I forgot to copy the code to a new sheet I had already
    completed the CF on. Once I insert the code, no problems until recently. I make the CF pertain to the entire range I need to ID row/column's.
    How does the code constantly update the CF when I move the cursor to a new cell????

  18. #18
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA working with conditional formatting

    hi Markstro, hope I understood everything right from your long thread. The problem is with CF not working correctly (highlighting selected row from the left and column from top) in 2007 except by pressing F9 each time. It will work correctly as soon as you put the following code in This Workbook module level:
    Please Login or Register  to view this content.
    If you do not get required highlighting, run the following first:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by watersev; 01-06-2012 at 06:33 AM.

  19. #19
    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,447

    Re: VBA working with conditional formatting

    @watersev: oh wow! I had no idea you could do that ... thought the selection change code was redundant

    Regards, TMS

  20. #20
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Ok guys, thanks for the support.
    Just trying to digest what you're all telling me, Maybe it's time to move up the knowledge level on how to use VBA.
    I'll post an update soon as I can.

  21. #21
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Express Chipping Invoice Reconciliation.xlsm
    watersev, I've attached my file with your code inserted, still no joy. F9 works though.
    What am I missing in my description of my system or situation in general.
    I spent enough time debuggig this VBA/CF a couple of years ago and thought I would never have
    to go through again.

  22. #22
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    One more clue that may help, after enter new data in a cell, hit return and the CF kicks in.
    I am still not sure just what part of the code does that, the Application.ScreenUpdating = true??????

  23. #23
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Thumbs up Re: VBA working with conditional formatting

    the following screenshot is taken from Excel 2007, your file from post #21

    Please do the following code: open posted file, run code
    Please Login or Register  to view this content.
    Save and close file. Open it up. It should work exactly as on the screenshot attached without F9 pressing.
    Attached Images Attached Images

  24. #24
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA working with conditional formatting

    @MARKSTRO

    The trick is done by: application.screenupdating =1 sitting in This Workbook module level in Workbook_SheetSelectionChange. It forces CF to be calculated though a cell is selected only.

    For the events to be happening you need to EnableEvents, see previous post.

  25. #25
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    I'll get on this, thanks.
    Then how did it work as it was all this time???

  26. #26
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA working with conditional formatting

    as soos as you (or some code) set EnableEvents to False - turn off, it is set until revert its value to true

  27. #27
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    vba screen shot 1-12-12.jpeg
    Please see attached image
    Is this what the VBA is supposed to look like.
    If so, no joy.
    Thanks,

  28. #28
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    OOps, wrong screen shot, the correct one has the code you posted in post # 18

  29. #29
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA working with conditional formatting

    Message Box testing
    1.Close and save any open workbooks, and then create a new workbook.
    2.Start the Visual Basic Editor (press ALT+F11).
    3.If the Project Explorer window is not visible, click Project Explorer on the View menu.
    4.In the Project Explorer window, double-click Sheet1 to open the Code window for Sheet1.
    5.In the Code window for Sheet1, type or paste the following code for the SelectionChange event:
    Please Login or Register  to view this content.
    6.Switch to Microsoft Excel (ALT+F11).
    7.Select any cell on Sheet1.

    Does the message box show up with cell address?

    If no, please open attachment file, run code "EnableEvents". How to Run a code:

    1.If the Developer tab is not available, do the following to display it:
    - click the Microsoft Office Button , and then click Excel Options.
    - in the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check box, and then click OK.
    2.To set the security level temporarily to enable all macros, do the following:
    - on the Developer tab, in the Code group, click Macro Security.
    - in the Macro Settings category, under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
    3.Open attached file.
    4.On the Developer tab, in the Code group, click Macros.
    5.In the Macro name box, click the macro "EnableEvents".
    6.Click Run.

    Check Sheet("2011") of the attachment, select any cell, conditional format should work as expected.
    Attached Files Attached Files

  30. #30
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Both sheets with differents VBA setups work now, problem may be solved, I have attached screen shots of the VBA, your version has the Sub Enable.Events code in Module 1 and mine is in the This Workbook is the only difference I can see.
    your sheet screen shot 1-12-12.gifyour sheet module screen shot 1-12-12.gifmy sheet screen shot 1-12-12.gif
    I hope I presented all this for you to review.
    My other question remains about why this worked in my XP system without the Sub Enable.Events code, why is it needed now to work on Windows 7?

  31. #31
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA working with conditional formatting

    it has nothing to do with the operating system, the only thing that matters if events are enabled or not. So now you know how to enable events in case conditional format is not working.

    On XP it worked because events were on, turn them off and it will behave the same as W7, on W7 events were off by some code or manually.

    Note: If some code turns the events off, they will be turned off (even if you restart Excel) until some code will turn them on again.
    Last edited by watersev; 01-12-2012 at 05:00 PM.

  32. #32
    Forum Contributor
    Join Date
    10-27-2009
    Location
    San Diego California
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: VBA working with conditional formatting

    Great, makes sense. Problem Solved.
    Does where the Enable.Event code is inserted; in the module or ThisWorkbook make a difference?

+ 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