welshgasman
Well-known Member
- Joined
- May 25, 2013
- Messages
- 1,395
- Office Version
- 2019
- 2007
- Platform
- 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?
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?
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