根据值Excel VBA更改ListView中的原色

时间:2018-11-10 02:32:31

标签: excel vba excel-vba

希望您能对我有所帮助...我的列表视图中显示了数据。标题栏为:

行ID客户取件交付位置放置袋的数量状态--总共10列

我希望列 STATUS 中的前景色取决于该值。值为 PAID UNPAID ,如果 PAID ,颜色应为绿色,如果为 UNPAID ,则颜色应为是红色的。

我有此代码,但对我不起作用,有人会帮助我吗?预先谢谢你。

Private Sub UserForm_Activate()

Dim C As Long
Dim i As Long
Dim R As Long

ListView1.View = lvwReport
ListView1.HideSelection = False
ListView1.FullRowSelect = True
ListView1.HotTracking = True
ListView1.HoverSelection = False

ListView1.ColumnHeaders.Add Text:="Row", Width:=40

  For C = 1 To 12
    ListView1.ColumnHeaders.Add Text:=Cells(1, C).Text
    ComboBox1.AddItem Cells(1, C).Text
  Next C

**' |In this part of my code is not working|**
Dim Item    As ListItem
Dim counter As Long

For counter = 1 To listView1.ListItems.Count
    Set Item = listView1.ListItems.Item(counter)
    If Item.SubItems(10) = "Paid" Then
        listView1.ListItems.Item(counter).ListSubItems(10).ForeColor = vbGreen
    End If
    If Item.SubItems(10) = "Unpaid" Then
        listView1.ListItems.Item(counter).ListSubItems(10).ForeColor = VBRed


Next counter


End Sub

1 个答案:

答案 0 :(得分:0)

listsubitime必须为9,因为索引号从0开始。 在我的测试中,它运行良好。

enter image description here

Private Sub UserForm_Activate()

Dim C As Long
Dim i As Long
Dim R As Long
Dim li As ListItem

ListView1.View = lvwReport
ListView1.HideSelection = False
ListView1.FullRowSelect = True
ListView1.HotTracking = True
ListView1.HoverSelection = False

ListView1.ColumnHeaders.Add Text:="Row", Width:=40

  For C = 1 To 12
    ListView1.ColumnHeaders.Add Text:=Cells(1, C).Text
    ComboBox1.AddItem Cells(1, C).Text
  Next C
Dim vDB
vDB = Range("a1").CurrentRegion
For i = 2 To UBound(vDB, 1)
    Set li = ListView1.ListItems.Add
    For j = 1 To UBound(vDB, 2)
    With li
        .Text = i
        .ListSubItems.Add , , vDB(i, j)
    End With
    Next j
Next i
'**' |In this part of my code is not working|**
Dim Item    As ListItem
Dim counter As Long

For counter = 1 To ListView1.ListItems.Count
    Set Item = ListView1.ListItems.Item(counter)
    If Item.SubItems(9) = "Paid" Then
        ListView1.ListItems.Item(counter).ListSubItems(9).ForeColor = vbGreen
    End If
    If Item.SubItems(9) = "Unpaid" Then
        ListView1.ListItems.Item(counter).ListSubItems(9).ForeColor = vbRed
    End If

Next counter


End Sub