简体   繁体   English

遍历行并为每行应用不同的条件格式

[英]Looping through rows and apply different conditional formatting for each row

This script looks up the value in the first column in a row, finds the corresponding value in a second sheet and gets values from the other columns in the same row in the second sheet. 该脚本在一行的第一列中查找值,在第二张工作表中查找对应的值,并从第二张工作表的同一行中的其他列获取值。 Then, it applies conditional formatting on values in the first sheet according to the values retrieved from the second sheet. 然后,它根据从第二张工作表中检索到的值对第一张工作表中的值应用条件格式。

However, i can only get it to work on one row at a time, and I don't wish to repeat the code for all the rows in the first sheet. 但是,我一次只能使它在一行上工作,并且我不希望为第一张纸上的所有行重复该代码。 How can I loop through all rows in the first sheet and do the same thing for the remaining rows? 如何遍历第一张工作表中的所有行,并对其余行做同样的事情?

Sub Vlookup4()

Dim FndStr As String
Dim FndVal As Range
Dim FndRng As Range
Dim Ul1 As Double, Ul2 As Double, Ul3 As Double, Ul4 As Double, Ul5 As Double

    FndStr = Range("A10").Value

    Set FndVal = Worksheets("Grenseverdier_jord").Columns("A:A").Find(What:=FndStr, LookAt:=xlWhole)
        Ul1 = FndVal.Offset(0, 1).Value
        Ul2 = FndVal.Offset(0, 2).Value
        Ul3 = FndVal.Offset(0, 3).Value
        Ul4 = FndVal.Offset(0, 4).Value
        Ul5 = FndVal.Offset(0, 5).Value

    Set FndRng = Range(Cells(10, 3), Cells(10, Cells(10, Columns.Count).End(xlToLeft).Column))

    Debug.Print FndRng.Address

    With ActiveSheet

        With FndRng
            .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C10);C10<" & Ul1 & ")"
            .FormatConditions(1).Interior.ColorIndex = 33
            .FormatConditions(1).Borders.LineStyle = xlContinuous
            .FormatConditions(1).Borders.Weight = xlThin

            .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C10);C10>=" & Ul1 & ";C10<" & Ul2 & ")"
            .FormatConditions(2).Interior.ColorIndex = 4
            .FormatConditions(2).Borders.LineStyle = xlContinuous
            .FormatConditions(2).Borders.Weight = xlThin

            .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C10);C10>=" & Ul2 & ";C10<" & Ul3 & ")"
            .FormatConditions(3).Interior.ColorIndex = 6
            .FormatConditions(3).Borders.LineStyle = xlContinuous
            .FormatConditions(3).Borders.Weight = xlThin

            .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C10);C10>=" & Ul3 & ";C10<" & Ul4 & ")"
            .FormatConditions(4).Interior.ColorIndex = 45
            .FormatConditions(4).Borders.LineStyle = xlContinuous
            .FormatConditions(4).Borders.Weight = xlThin

            .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C10);C10>=" & Ul4 & ";C10<" & Ul5 & ")"
            .FormatConditions(5).Borders.LineStyle = xlContinuous
            .FormatConditions(5).Borders.Weight = xlThin
            .FormatConditions(5).Interior.ColorIndex = 3

            .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C10);C10>=" & Ul5 & ")"
            .FormatConditions(6).Interior.ColorIndex = 7
            .FormatConditions(6).Borders.LineStyle = xlContinuous
            .FormatConditions(6).Borders.Weight = xlThin

            .FormatConditions.Add xlExpression, Formula1:="=LEFT(C10;1)=""<"""
            .FormatConditions(7).Interior.ColorIndex = 33
            .FormatConditions(7).Borders.LineStyle = xlContinuous
            .FormatConditions(7).Borders.Weight = xlThin

            .FormatConditions.Add xlExpression, Formula1:="=(C10) = ""n.d."""
            .FormatConditions(8).Interior.ColorIndex = 33
            .FormatConditions(8).Borders.LineStyle = xlContinuous
            .FormatConditions(8).Borders.Weight = xlThin

        End With
        End With
End Sub

I'm attaching a sample file for testing. 我正在附上样本文件进行测试。

Sample file 样本文件

This should do it for you: 这应该为您做:

Sub Vlookup4() 子Vlookup4()

Dim FndStr As String
'Dim FndVal As Range
Dim FndRng As Range
Dim Ul1 As Double, Ul2 As Double, Ul3 As Double, Ul4 As Double, Ul5 As Double
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A

For i = 10 To LastRow
    FndStr = ws.Range("A" & i).Value

    Set FndVal = Worksheets("Grenseverdier_jord").Columns("A:A").Find(What:=FndStr, LookAt:=xlWhole)
        If Not FndVal Is Nothing Then
                Ul1 = FndVal.Offset(0, 1).Value
                Ul2 = FndVal.Offset(0, 2).Value
                Ul3 = FndVal.Offset(0, 3).Value
                Ul4 = FndVal.Offset(0, 4).Value
                Ul5 = FndVal.Offset(0, 5).Value

            Set FndRng = ws.Range("C" & i & ":I" & i)

            With ActiveSheet

                With FndRng

                    .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C " & i & ");C " & i & "<" & Ul1 & ")"
                    .FormatConditions(1).Interior.ColorIndex = 33
                    .FormatConditions(1).Borders.LineStyle = xlContinuous
                    .FormatConditions(1).Borders.Weight = xlThin

                    .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C " & i & ");C " & i & ">=" & Ul1 & ";C " & i & "<" & Ul2 & ")"
                    .FormatConditions(2).Interior.ColorIndex = 4
                    .FormatConditions(2).Borders.LineStyle = xlContinuous
                    .FormatConditions(2).Borders.Weight = xlThin

                    .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C " & i & ");C " & i & ">=" & Ul2 & ";C " & i & "<" & Ul3 & ")"
                    .FormatConditions(3).Interior.ColorIndex = 6
                    .FormatConditions(3).Borders.LineStyle = xlContinuous
                    .FormatConditions(3).Borders.Weight = xlThin

                    .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C " & i & ");C " & i & ">=" & Ul3 & ";C " & i & "<" & Ul4 & ")"
                    .FormatConditions(4).Interior.ColorIndex = 45
                    .FormatConditions(4).Borders.LineStyle = xlContinuous
                    .FormatConditions(4).Borders.Weight = xlThin

                    .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C " & i & ");C " & i & ">=" & Ul4 & ";C " & i & "<" & Ul5 & ")"
                    .FormatConditions(5).Borders.LineStyle = xlContinuous
                    .FormatConditions(5).Borders.Weight = xlThin
                    .FormatConditions(5).Interior.ColorIndex = 3

                    .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(C " & i & ");C " & i & ">=" & Ul5 & ")"
                    .FormatConditions(6).Interior.ColorIndex = 7
                    .FormatConditions(6).Borders.LineStyle = xlContinuous
                    .FormatConditions(6).Borders.Weight = xlThin

                    .FormatConditions.Add xlExpression, Formula1:="=LEFT(C " & i & ";1)=""<"""
                    .FormatConditions(7).Interior.ColorIndex = 33
                    .FormatConditions(7).Borders.LineStyle = xlContinuous
                    .FormatConditions(7).Borders.Weight = xlThin

                    .FormatConditions.Add xlExpression, Formula1:="=(C " & i & ") = ""n.d."""
                    .FormatConditions(8).Interior.ColorIndex = 33
                    .FormatConditions(8).Borders.LineStyle = xlContinuous
                    .FormatConditions(8).Borders.Weight = xlThin
                End With
            End With
        End If
Next i
End Sub

I've added a line to find the LastRow and then used a For Loop to loop through each row, I've also added If Not FndVal Is Nothing Then to make sure that if nothing is found on the other sheet it doesn't cause an error. 我添加了一行以查找LastRow,然后使用For循环遍历每一行,并且还添加了If Not FndVal Is Nothing Then ,以确保如果在另一张纸上未找到任何内容,则不会一个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有条件的单元格填充代码适用于几行,每行条件不同 - Conditional cell fill code to apply to several rows with different conditions for each row 将条件格式应用于许多行 - Apply conditional formatting to many rows Excel条件格式行,每行具有唯一的公式 - Excel Conditional Formatting Rows with Formula unique to each row 通过行比较在Excel中对两行进行条件格式化 - Conditional formatting of two rows in Excel through row comparison 遍历工作表并将每行的最后一行粘贴到不同的工作表中 - Looping through worksheets and pasting the last rows of each into a different sheet VBA/EXCEL:循环遍历具有条件格式的单元格以隐藏基于 ColorIndex 的行 - VBA/EXCEL: Looping Through Cells With Conditional Formatting To Hide Rows Based on ColorIndex 遍历电子表格中的行,在每一行中运行一个函数,然后将结果输出回同一行的末尾 - Looping through rows from a spreadsheet, running a function through each row and outputting the result back to the end of the same row 如何将条件格式设置规则应用于除第一行以外的所有行? - How can i apply a conditional formatting rule to all rows except first row? 许多的每一行中最大值的条件格式 - Conditional Formatting of maximum value in each row of many 基于Excel中每行中不确定行数确定的值的条件格式 - Conditional formatting based on a value determined in each row in Excel for indeterminate amount of rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM