我试图循环遍历可变数量的行,在列E中搜索值(即“WLO”)。当我在列E中找到带有“WLO”的行时,我想从列J中选择该行中的单元格,直到该行中的数据结束。列J之后填充的列数会有所不同,因为这在多个电子表格中使用,因此右侧的选择需要是动态的。我想做类似下面的代码,但是当前它将WLO的第一个实例之间的每一行转换为WLO红色的最后一个实例,即使其中包含其他值的行也是如此。 See Screenshot of current results here.
Sub Color_row()
Dim EndRow As Long
Dim e As Long
EndRow = Range("E" & Rows.Count).End(xlUp).Row
For e = 2 To EndRow
If (Range("E" & e).Value = "WLO") Then
Range("J" & e, Selection.End(xlToRight)).Interior.ColorIndex = 3
End If
Next e
End Sub
This is an example of I would like to be able to do 谢谢你的建议。非常感谢!
答案 0 :(得分:0)
您需要先创建一个结束范围。你很亲密!
Sub Color_row()
Dim EndRow As Long
Dim e As Long
Dim EndCol, StartCol As Range
EndRow = Range("E" & Rows.Count).End(xlUp).Row
For e = 2 To EndRow
If (Range("E" & e).Value = "WLO") Then
Set EndCol = Range("J" & e).End(xlToRight)
Set StartCol = Range("J" & e)
Range(StartCol, EndCol).Interior.ColorIndex = 3
End If
Next e
End Sub
答案 1 :(得分:0)
在Then子句中,Selection是活动单元格所在的位置,因此它将突出显示该行。将选择更改为您的起点,即J和行号。
Sub Color_row()
Dim EndRow As Long
Dim e As Long
EndRow = Range("E" & Rows.Count).End(xlUp).Row
For e = 2 To EndRow
If (Range("E" & e).Value = "WLO") Then
Range("J" & e, Range("J" & e).End(xlToRight)).Interior.ColorIndex = 3
End If
Next e
End Sub