循环访问代码,返回E或F的3个值以及A,B,C或D的2个值

我期待执行一个代码,它将返回“E”或“F”的三个值,并返回A,B,C或D的2个值。这是我目前为止的内容。

Sheets("123").Select Dim rng As Range Dim count As Integer Set rng = Range("L2:L500000") For Each cell In rng If Left(cell.Value, 1) = "A" Or Left(cell.Value, 1) = "B" Or _ Left(cell.Value, 1) = "C" Or Left(cell.Value, 1) = "D" Or _ Left(cell.Value, 1) = "E" Or Left(cell.Value, 1) = "F" Then cell.EntireRow.Interior.ColorIndex = 4 count = count + 1 End If If count >= 5 Then Exit For Next 

你需要保留两个计数器,一个用于A,B,C,D,另一个用于E,F。

根据你的问题(而不是你的评论中给出的矛盾数字),下面应该做你想要的:

 Dim cell As Range Dim countABCD As Integer Dim countEF As Integer For Each cell In Worksheets("123").Range("L2:L500000") Select Case Left(cell.Value, 1) Case "A", "B", "C", "D" If countABCD < 2 Then cell.EntireRow.Interior.ColorIndex = 4 countABCD = countABCD + 1 End If Case "E", "F" If countEF < 3 Then cell.EntireRow.Interior.ColorIndex = 4 countEF = countEF + 1 End If End Select If countABCD = 2 And countEF = 3 Then Exit For End If Next