英文:
Highlight the row if the above active cell matches the bottom
问题
Here's the translated code part:
子重复颜色()
Dim i As Long, cIndex As Long
cIndex = 3
Cells(1, 1).Interior.ColorIndex = cIndex
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1) = Cells(i + 1, 1) Then
Cells(i + 1, 1).Interior.ColorIndex = cIndex
Else
If Cells(i + 1, 1) <> "" Then
cIndex = cIndex + 1
Cells(i + 1, 1).Interior.ColorIndex = cIndex
End If
End If
Next i
End Sub
If you need further assistance or have any questions, please let me know.
英文:
Sub dupColors()
Dim i As Long, cIndex As Long
cIndex = 3
Cells(1, 1).Interior.ColorIndex = cIndex
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1) = Cells(i + 1, 1) Then
Cells(i + 1, 1).Interior.ColorIndex = cIndex
Else
If Cells(i + 1, 1) <> "" Then
cIndex = cIndex + 1
Cells(i + 1, 1).Interior.ColorIndex = cIndex
End If
End If
Next i
End Sub
I got rid of cIndex
, not needed. Basically I want to highlight groups of CUSIP numbers which are always different and the data range is dynamic. The way i go about it is by sorting the cusip datarange so that they are all grouped.
I need a script that would highlight each group then alternate to another color when they do not match. The issue is that the cell numbers can jump and therefore any i+1 functions do not work. Any ideas?
Regards
I tried an if function where it first states the range then does a count up to see the last cell that was input. From there the function would jump up (i + 1) and highlight if it was the same string. If not it would go to the next.
答案1
得分: 2
以下是翻译好的代码部分:
Sub dupColors()
Dim i As Long, n As Long, arrColors, currVal, v
arrColors = Array(24, 40) '或其他值
currVal = Chr(0) '非值
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
v = Cells(i, 1)
If v <> currVal Then
currVal = v
n = n + 1
End If
' n Mod 2 将为 0 或 1...
Cells(i, 1).Interior.ColorIndex = arrColors(n Mod 2)
Next i
End Sub
如果您需要进一步的帮助,请随时提问。
英文:
Try this out:
Sub dupColors()
Dim i As Long, n As Long, arrColors, currVal, v
arrColors = Array(24, 40) 'or whatever
currVal = Chr(0) 'non-value
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
v = Cells(i, 1)
If v <> currVal Then
currVal = v
n = n + 1
End If
' n Mod 2 will be 0 or 1...
Cells(i, 1).Interior.ColorIndex = arrColors(n Mod 2)
Next i
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论