+ Reply to Thread
Results 1 to 7 of 7

Function / VBA to avoid manually hidden cells (rows) for conditional formatting

  1. #1
    Registered User
    Join Date
    11-02-2022
    Location
    London, England
    MS-Off Ver
    Office 365, Version 2209
    Posts
    9

    Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    Hello,

    I have large spreadsheet that contains multiple data. Want to apply conditional formatting on the table so visually can segregate the various products for each line (sort A to Z on column D).
    The function (column H) / conditional formatting rule work fine if the data is just sorted, but as soon as it's filtered (in this case want to be filtered by each line (column E), the function (column H) is calculating the hidden cells and the conditional formatting is wrong.

    Is there any way this to be solved with function or maybe VBA so when the data is filtered / the rows manually hidden the function / condittional formatting to segregate the table?

    Screenshot 2023-07-07 011046.jpg
    Screenshot 2023-07-07 011156.jpg
    Screenshot 2023-07-07 012032.jpg
    Screenshot 2023-07-07 012112.jpg
    Attached is the example spreadsheet.

    Thank you in advance,
    Nik
    Attached Files Attached Files

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    My solution is to use VBA to trigger an event whenever the autofilter changes and update the border style and color to differentiate the items in column D.
    Unfortunately, to trigger the autofilter, we need to use the Calculate event.
    However, if there are no cells containing formulas in the worksheet, this event will not run when the filter is changed.
    In the example file, I temporarily added a formula =1+1 to cell J1. If your actual file already has at least one formula, you can skip this step.

    First, remove all existing conditional formatting formulas, also the helper column with true/false, and then paste the following code into the worksheet module:

    PHP Code: 
    Private Sub Worksheet_Calculate()
    Dim lr&, i&, j&, arr(), item As String
    lr 
    Range("A1000").End(xlUp).Row
    ReDim arr
    (1 To lr 11 To 2)
    For 
    2 To lr
        
    If Not Rows(i).Hidden Then
            k 
    1arr(k1) = i
            
    If Cells(i"D") <> item Then arr(k2) = True
            item 
    Cells(i"D").Value
        End 
    If
    Next
    For 1 To k
        With Cells
    (arr(i1), "A").Resize(16)
            .
    Borders(xlEdgeTop).LineStyle IIf(arr(i2), xlContinuousxlDot)
            .
    Borders(xlEdgeBottom).LineStyle xlDot
            
    .Borders(xlEdgeTop).ColorIndex IIf(arr(i2), 31)
            .
    Borders(xlEdgeBottom).ColorIndex 1
            
    .Borders.Weight xlThin
        End With
    Next
    End Sub 
    Attached Files Attached Files
    Quang PT

  3. #3
    Registered User
    Join Date
    11-02-2022
    Location
    London, England
    MS-Off Ver
    Office 365, Version 2209
    Posts
    9

    Re: Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    Hi bebo021999,

    This is very high level for me and I can't adjust the vba code to work on my actual file .
    Attached is the similar form as the original - the file has a lot of formulas, so the calculation even is fine.


    Can you please help me to adjust the VBA to trigger an event whenever the autofilter changes and update the border style and color to differentiate the items in column H.
    Also, I have noticed when the table is filtered / autofilter, the red borderline (the one that is dividing the product) is not visible between the hidden files, can you please look on that as well, because want always they to be divided whenever is auto filtered.

    I appreciate your help and work a lot.

    Regards,
    Nik
    Attached Files Attached Files

  4. #4
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    Try again. Does it work?
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-02-2022
    Location
    London, England
    MS-Off Ver
    Office 365, Version 2209
    Posts
    9

    Re: Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    Hi bebo021999,

    The code fit good on my original file, but still is not working properly.
    When the filter is not applied works all good, but when is the filter is applied is not.

    Here is example of non-filtered (the red solid border line is dividing the rows when the 'Item code' column is different and black dashed border line when the 'Item code' column is same):
    Screenshot 2023-07-13 111603.png
    Here is example of filtered (the red solid line and black dashed border line are randomly applied + here are a lot of solid black border line as well):
    Screenshot 2023-07-13 111453.png
    Can this be fixed?

    Regards,
    Nik

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,916

    Re: Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    Administrative Note:

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so.

    Please see Forum Rule #3 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important: https://excelguru.ca/a-message-to-forum-cross-posters/

    (Note: this requirement is not optional. I shall do it for you ONCE ONLY: https://www.mrexcel.com/board/thread...anges.1241306/)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Registered User
    Join Date
    11-02-2022
    Location
    London, England
    MS-Off Ver
    Office 365, Version 2209
    Posts
    9

    Re: Function / VBA to avoid manually hidden cells (rows) for conditional formatting

    Hi AliGW,

    I truly understand the frustration, please accept my apology!
    I have posted new thread, this time properly and my intention is to look for more experts to solve my issue on my spreadsheet.

    Regards,
    Nik

+ 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. SUMIF function for manually hidden Cells?
    By DonRogallo80 in forum Excel General
    Replies: 4
    Last Post: 03-08-2023, 10:04 PM
  2. Conditional Formatting with Hidden Rows
    By quark20 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2019, 10:42 PM
  3. Replies: 10
    Last Post: 08-21-2017, 12:19 PM
  4. Use conditional formatting only on visible not hidden cells
    By JackBauer in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-24-2014, 08:26 AM
  5. Replies: 1
    Last Post: 08-12-2013, 01:57 PM
  6. [SOLVED] How to avoid copying Conditional Formatting to other cells
    By amarus99 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-20-2012, 02:45 PM
  7. Replies: 3
    Last Post: 05-15-2012, 04:13 PM

Tags for this Thread

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