Conditional Formatting with VBA getting incorrect row values in final CF

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
1,402
Office Version
  1. 2019
  2. 2007
Platform
  1. Windows
Hi all,
I had some help using Conditional Formatting in VBA in this thread. VBA apply conditional formatting

I have still not been able to work out what is going on. :-(
Does anyone have any clue please.

Basically Excel is replacing my target range with a very high number, near the limit of Excel rows.
I have walked through the code and checked that the values from the worksheet cells are passed in correctly, but those high numbers still get in there.

If I cannot get to the bottom of it, would anyone know a workaround?

1687800283285.png
1687800312685.png


Code:
Sub Set_CF()
' https://www.mrexcel.com/board/threads/vba-apply-conditional-formatting.1220882/
' Created: 20221101
' By:      HaHoBe
' Version: 1
' Updated: 20221117
' Reason:  allowing more than one condition for CF
    Dim lngCounter As Long
    Dim rngToWork As Range
    Dim ws As Worksheet
    Dim wsCond As Worksheet
    Dim wsTarg As Worksheet

    On Error GoTo Error_CF
    Set wsCond = Worksheets("CF")
    If wsCond Is Nothing Then GoTo end_here
    On Error GoTo 0

    'Remove old conditional formatting
    '  For Each Ws In ThisWorkbook.Worksheets
    '    If Ws.Name <> wsCond.Name Then
    '      Ws.Cells.FormatConditions.Delete
    '    End If
    '  Next Ws
    ' Clear formatting
    '/// I would recommend to use the tab name here instead of any position
    Worksheets("SF66OEK").Cells.FormatConditions.Delete
    Worksheets("Passengers").Cells.FormatConditions.Delete
   
    'Set new formatting
    For lngCounter = 2 To wsCond.Range("A" & Rows.Count).End(3).Row
        On Error GoTo Error_CF
        Set wsTarg = Worksheets(wsCond.Cells(lngCounter, 1).Value)
        If wsTarg Is Nothing Then GoTo end_here
        Set rngToWork = wsTarg.Range(wsCond.Cells(lngCounter, 6).Value, wsCond.Cells(lngCounter, 7).Value)
        If rngToWork Is Nothing Then GoTo end_here
        On Error GoTo 0
        With rngToWork
            Debug.Print wsCond.Cells(lngCounter, 3).Value
            .FormatConditions.Add xlExpression, Formula1:=wsCond.Cells(lngCounter, 3).Value
            .FormatConditions(wsCond.Cells(lngCounter, 8).Value).Font.ColorIndex = wsCond.Cells(lngCounter, 5).Value
            .FormatConditions(wsCond.Cells(lngCounter, 8).Value).Interior.Color = wsCond.Cells(lngCounter, 4).Value
            .FormatConditions(wsCond.Cells(lngCounter, 8)).StopIfTrue = False

        End With
    Next lngCounter

end_here:
    If wsCond Is Nothing Then
        MsgBox "Check the name of the sheet with the parameters.", vbExclamation, "Name of sheet with parameters"
    ElseIf wsTarg Is Nothing Then
        MsgBox "Check the name of the target sheet", vbExclamation, "Could not find sheet"
    ElseIf rngToWork Is Nothing Then
        MsgBox "Could not build a range to work on, please check addresses.", vbExclamation, "Problems building range"
    End If

    Set rngToWork = Nothing
    Set wsTarg = Nothing
    Set wsCond = Nothing
    MsgBox "CF now reset on " & ActiveWorkbook.Name
    Exit Sub
Error_CF:
    MsgBox "Error in CF module " & Err.Description & " - " & Err.Number
    Resume end_here
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Why are you looping through each cell in your range separately and applying Conditional Formatting rules for each cell individually?
You can apply Conditional Formatting to a whole range of cells at one time without having to loop through each cell in the range to do it.
 
Upvote 0
TBH Joe,
I just accepted the code as it was given.
I have modified it a little now trying to get to the bottom of this issue, but the logic remains the same?

How would you apply the formatting then?

Edit: I do not believe I am, that is walking through the rows in the CF sheet that holds the settings for the CF? See last pic?
The range is rngtoWork which when debugging is correct?
 
Upvote 0
OK, I think maybe I misunderstood the initial question.
So, is it that you have a listing of rules that you want to create listed on an Excel sheet, and you are looping through those specifications to create the various Conditional Formatting Rules via VBA?

If so, can you re-post the Excel grid of formula rules either by using the XL2BB utility, or using Copy/Paste, so I can easily copy those over so I can work off of the same data you are?
I really not prefer to type each of those in manually. Besides being a slow typist, there is also a good chance that I may make some typos!
 
Upvote 0
Yes to both.
The reason I am doing this is when I need extra rows and copy the formulae, I end up with multiple CFs of the same criteria and settings.
Just trying to get the onse set per sheet.

GCD.xlsm
ABCDEFGHIJ
1SheetCellRule TypeRule RescriptionFill ColourFont ColourApplies FromApplies ToOrderColourSortOrder
2SF66OEKFormula:==Left($E4,9) = "Cancelled"167772151$A4$L731Red1A
3SF66OEKFormula:==$K4 = "Yes"167772154$A4$L732Green2A
4SF66OEKFormula:==AND($H4=$J4,$G4<>"")1677721526$A4$L733Magenta3A
5SF66OEKFormula:==$O4 > 0652801$A4$L734Green4A
6PassengersFormula:==$L2 = "No"167772153$A2$L5001Red1B
CF
 
Upvote 0
Hmmm... When I copied those rules and the code, I could not recreate the issue you mention about the high number rows.
Are you inserting, deleting, or copying rows after the fact?
 
Upvote 0
Just discovered that the last row (6) which is for a different sheet has the correct values for the criteria as per the CF sheet?
 
Upvote 0
No.
The sheet starts off with a set number of rows, which really depends on the previous month.
However if it was a quiet month then there might just be 30 rows.
Then the current month needs more rows, so I add 5 at a time with VBA.
That code is below.

Code:
Sub InsertRows()
'
' InsertRows Macro
'
Dim intActiveRow As Integer, intInsert As Integer, i As Integer, intCopyRow As Integer
intInsert = 5
intActiveRow = ActiveCell.Row
intCopyRow = ActiveCell.Row - 1

    Range("A" & intActiveRow & ":N" & intActiveRow).Select
    Selection.Copy
    For i = 1 To intInsert
        Selection.Insert Shift:=xlDown
        Rows(intActiveRow + i).RowHeight = 30
    Next
    intActiveRow = intActiveRow + intInsert

    ' Now copy the formulae
    Range("F" & intCopyRow).Copy
    Range("F" & intCopyRow + 1 & ":F" & intActiveRow).PasteSpecial , xlPasteSpecialOperationNone
    Range("H" & intCopyRow & ":N" & intCopyRow).Copy
    Range("H" & intCopyRow + 1 & ":N" & intActiveRow).PasteSpecial , xlPasteSpecialOperationNone
    Call SetPrintArea
End Sub

So if it works for you, then there must be something wrong with the actual sheet, as I have just noticed that the Passenger sheet had the CF as per the CF row for that sheet.

Thanks for testing that anyway.
 
Upvote 0
No.
The sheet starts off with a set number of rows, which really depends on the previous month.
However if it was a quiet month then there might just be 30 rows.
Then the current month needs more rows, so I add 5 at a time with VBA.
That code is below.

Code:
Sub InsertRows()
'
' InsertRows Macro
'
Dim intActiveRow As Integer, intInsert As Integer, i As Integer, intCopyRow As Integer
intInsert = 5
intActiveRow = ActiveCell.Row
intCopyRow = ActiveCell.Row - 1

    Range("A" & intActiveRow & ":N" & intActiveRow).Select
    Selection.Copy
    For i = 1 To intInsert
        Selection.Insert Shift:=xlDown
        Rows(intActiveRow + i).RowHeight = 30
    Next
    intActiveRow = intActiveRow + intInsert

    ' Now copy the formulae
    Range("F" & intCopyRow).Copy
    Range("F" & intCopyRow + 1 & ":F" & intActiveRow).PasteSpecial , xlPasteSpecialOperationNone
    Range("H" & intCopyRow & ":N" & intCopyRow).Copy
    Range("H" & intCopyRow + 1 & ":N" & intActiveRow).PasteSpecial , xlPasteSpecialOperationNone
    Call SetPrintArea
End Sub

So if it works for you, then there must be something wrong with the actual sheet, as I have just noticed that the Passenger sheet had the CF as per the CF row for that sheet.

Thanks for testing that anyway.
OK, so you hadn't included that "InsertRows" code in your initial post.
After you run that, I believe that you will need to run the "Set_CF" code again in order to reset everything properly.

Also note that if you have any "Worksheet_Change" or "Worksheet_Selection" event procedure code in any of these worksheets, that could be interfering with things too.
 
Upvote 0
I have just copied the sheet in question and the CF to a new workbook with the code and now it starts at row 7, not 4 as in the code. :(
I will check those other events, but rarely use them TBH.

That is the whole point of this CF code to reset the entries to just what is in the CF sheet, as opposed to multiple CF entries for the same criteria, caused by copying the rows.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top