2016-12-06 28 views
0

我期待執行一個代碼,它將返回「E」或「F」的3個值並且也返回A,B,C或D的2個值。以下是我目前爲止的內容。循環代碼,返回E或F的3個值以及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 
+0

這不是真的清楚你的要求。 – SJR

+0

基本上,如果單元格範圍內有字母A,B,C,D,E或F,代碼將突出顯示5行。但是,我需要總共3行返回字母A,B,C或D和總共兩行字母E或F.該代碼也隨機對數據進行隨機排序(如預期),但會給我不同的組合第一組字母與第二組字母,即A行,B行,C行或D行以及E行或行行4行。 –

+0

您能舉一個預期的輸入和輸出示例嗎? –

回答

1

您需要保留兩個計數器,一個用於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 
+0

Upvoted爲了嘗試瞭解OP實際上需要什麼而努力工作這麼艱難的工作 – user3598756

+0

@ user3598756 - LOL - 強調**嘗試**! – YowE3K

+0

太棒了,太棒了,YowE3K。謝謝。 –

相關問題