条件格式也适用于不在范围内的电子表格。

huangapple go评论64阅读模式
英文:

Conditional formatting also applied to spreadsheets not in range

问题

I'm here to help with the translation. Please let me know which specific parts you'd like translated.

英文:

Introduction of what it should do:

I would like conditional formatting of value E in a spreadsheet if 2 conditions are met.

Term 1: The name of the worksheet gives the spreadsheet a classification which can be found in spreadsheet "macro" in Range C11-F100:

条件格式也适用于不在范围内的电子表格。

If the spreadsheet name is not in the table that no formatting should be performed on that spreadsheet.
If the spreadsheet name is in the table than is should look in which column it is for the class (A, B, C or D)

Than it should loop through the rows in that spreadsheet (starting row 2). If column G is "settle_plate", "active_plate" or "rodac_plate" is should check if value of column E is higher then Alert of Action limit in the row that corresponds with it spreadsheets classication fo term 1. If it is it should turn either orange or red.

条件格式也适用于不在范围内的电子表格。

Problem:
I only put in "settle_plate" so far (not "active_plate" and "rodac_plate") to see if it works so far. It applies the conditional formatting in the right way on the spreadsheets where it should do it. The only problem is that is also applies the conditional formatting on all other spreadsheets where it shouldn't do any condition formatting at all.
I now vloopup probably works better, but I am not familiar with that

 Dim rowNum As Long
    Dim macroSheet As Worksheet
    Dim macroRange As Range
    Dim macroCell As Range
    Dim useC5D5 As Boolean
    Dim useC6D6 As Boolean
    Dim useC7D7 As Boolean
    
    Set macroSheet = ThisWorkbook.Sheets("macro")
    Set macroRange = macroSheet.Range("C11:C100")
    
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "macro" And ws.Name <> "blad1" Then
            ' Clear existing conditional formatting rules
            ws.Cells.FormatConditions.Delete
            
            ' Check if the worksheet name matches the range C11:C100 in "macro" sheet
            useC5D5 = False
            For Each macroCell In macroRange
                If ws.Name = macroCell.Value Then
                    useC5D5 = True
                    Exit For
                End If
            Next macroCell
            
            ' If no match is found in C11:C100, check D11:D100
            If Not useC5D5 Then
                Set macroRange = macroSheet.Range("D11:D100")
                useC6D6 = False
                For Each macroCell In macroRange
                    If ws.Name = macroCell.Value Then
                        useC6D6 = True
                        Exit For
                    End If
                Next macroCell
             
                ' If no match is found in D11:D100, check E11:E100
                If Not useC6D6 Then
                    Set macroRange = macroSheet.Range("E11:E100")
                    useC7D7 = False
                    For Each macroCell In macroRange
                        If ws.Name = macroCell.Value Then
                            useC7D7 = True
                            Exit For
                        End If
                    Next macroCell
                End If
            End If
            
            ' Find the last used row in column G
            lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
            
            ' Loop through each row in column G
            For rowNum = 2 To lastRow
                ' Settle_plate = True and Column E >= C4 each row (case-insensitive) - Orange color
                If LCase(ws.Cells(rowNum, "G").Value) = LCase("settle_plate") Then
                    If useC5D5 Then
                        If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C4").Value Then
                            ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                        End If
                    ElseIf useC6D6 Then ' Settle_plate = True and Column E >= C5 each row (case-insensitive) - Orange color
                        If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C5").Value Then
                            ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                        End If
                    ElseIf useC7D7 Then ' Settle_plate = True and Column E >= C6 each row (case-insensitive) - Orange color
                        If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C6").Value Then
                            ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                        End If
                    Else ' Settle_plate = True and Column E >= C7 each row (case-insensitive) - Orange color
                        If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C7").Value Then
                            ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                        End If
                    End If
                End If
                
                If useC5D5 Then ' Settle_plate = True and Column E > D4 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D4").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                ElseIf useC6D6 Then ' Settle_plate = True and Column E > D5 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D5").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                ElseIf useC7D7 Then ' Settle_plate = True and Column E > D6 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D6").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                Else ' Settle_plate = True and Column E > D7 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D7").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                End If
            Next rowNum
        End If
    Next ws
End Sub

答案1

得分: 2

我认为问题在于你在代码中留下了较早版本的部分。

For rowNum = 2 To lastRow之后,有一个分支:If LCase(ws.Cells(rowNum, "G").Value) = LCase("settle_plate") Then,其中包含一个以If useC5D5 Then开头的部分。现在这部分在End If之后(循环内最外层的End If)几乎完全重复了一次:If useC5D5 Then ' Settle_plate = True and Column E >= D4 each row (case-insensitive) - Red color

它们之间有一个区别:第一个使用>=,第二个使用>

问题在于第二个部分会独立于工作表的名称而运行,并且由于所有的替代方案都包含一些格式化(涂成红色),因此总是会执行其中一个格式化命令。

或者你可能希望两个部分都存在,一个用于将事物涂成橙色,另一个用于将事物涂成红色。在这种情况下,将第二部分移到外部的if内部:

' Loop through each row in column G
For rowNum = 2 To lastRow
    ' Settle_plate = True and Column E >= C4 each row (case-insensitive) - Orange color
    If LCase(ws.Cells(rowNum, "G").Value) = LCase("settle_plate") Then
        If useC5D5 Then
            If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C4").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
            End If
        ElseIf useC6D6 Then ' Settle_plate = True and Column E >= C5 each row (case-insensitive) - Orange color
            If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C5").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
            End If
        ElseIf useC7D7 Then ' Settle_plate = True and Column E >= C6 each row (case-insensitive) - Orange color
            If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C6").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
            End If
        Else ' Settle_plate = True and Column E >= C7 each row (case-insensitive) - Orange color
            If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C7").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
            End If
        End If
        If useC5D5 Then ' Settle_plate = True and Column E > D4 each row (case-insensitive) - Red color
            If ws.Cells(rowNum, "E").Value > macroSheet.Range("D4").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
            End If
        ElseIf useC6D6 Then ' Settle_plate = True and Column E > D5 each row (case-insensitive) - Red color
            If ws.Cells(rowNum, "E").Value > macroSheet.Range("D5").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
            End If
        ElseIf useC7D7 Then ' Settle_plate = True and Column E > D6 each row (case-insensitive) - Red color
            If ws.Cells(rowNum, "E").Value > macroSheet.Range("D6").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
            End If
        Else ' Settle_plate = True and Column E > D7 each row (case-insensitive) - Red color
            If ws.Cells(rowNum, "E").Value > macroSheet.Range("D7").Value Then
                ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
            End If
        End If
    End If
Next rowNum
英文:

I think the problem is that you left parts of an earlier version in your code.

After For rowNum = 2 To lastRow there is an branch: If LCase(ws.Cells(rowNum, "G").Value) = LCase("settle_plate") Then which contains a part starting with If useC5D5 Then. Now this part is (almost exactly repeated after the End If (the outermost one inside the loop): If useC5D5 Then ' Settle_plate = True and Column E > D4 each row (case-insensitive) - Red color.

There is a difference between them: the first one uses >=, the second one >.

The problem is that the second one is run for each worksheet independent of its name, and one of the formatting commands is always executed since all alternatives contain some formatting (painting to red).

Or maybe you want both parts, one for coloring things orange and the other one for coloring things red. In this case, move the second part inside the outer if:

        ' Loop through each row in column G
        For rowNum = 2 To lastRow
            ' Settle_plate = True and Column E >= C4 each row (case-insensitive) - Orange color
            If LCase(ws.Cells(rowNum, "G").Value) = LCase("settle_plate") Then
                If useC5D5 Then
                    If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C4").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                    End If
                ElseIf useC6D6 Then ' Settle_plate = True and Column E >= C5 each row (case-insensitive) - Orange color
                    If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C5").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                    End If
                ElseIf useC7D7 Then ' Settle_plate = True and Column E >= C6 each row (case-insensitive) - Orange color
                    If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C6").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                    End If
                Else ' Settle_plate = True and Column E >= C7 each row (case-insensitive) - Orange color
                    If ws.Cells(rowNum, "E").Value >= macroSheet.Range("C7").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 165, 0) ' Orange color
                    End If
                End If
                If useC5D5 Then ' Settle_plate = True and Column E > D4 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D4").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                ElseIf useC6D6 Then ' Settle_plate = True and Column E > D5 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D5").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                ElseIf useC7D7 Then ' Settle_plate = True and Column E > D6 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D6").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                Else ' Settle_plate = True and Column E > D7 each row (case-insensitive) - Red color
                    If ws.Cells(rowNum, "E").Value > macroSheet.Range("D7").Value Then
                        ws.Cells(rowNum, "E").Interior.Color = RGB(255, 0, 0) ' Red color
                    End If
                End If
            End If
        Next rowNum

huangapple
  • 本文由 发表于 2023年6月8日 15:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定