EXCEL列表问题PRODUCE错误说“参数数目错误或属性分配无效”

lymnna71  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(115)
Sub Reset()
    Dim iRow As Long
    Dim ws As Worksheet
    Dim cell As Range
    
    Dim currentDate As Date
    Dim targetDate As Date
    Dim i As Long
    

    iRow = [Counta(Semua!A:A)] ' identifying the last row
    
    
    With frmForm
        .txtNoFile.Value = ""
        .txtTarikhNotis.Value = ""
        .txtUlasan.Value = ""
        .comboBoxStatus.Clear
        
        .comboBoxStatus.AddItem "Selesai"
        .comboBoxStatus.AddItem "Belum Selesai"
        
        
    
        .lstSemuaData.ColumnCount = 5
        .lstSemuaData.ColumnHeads = True
        
        .lstSemuaData.ColumnWidths = "90,90,90,90,100"
        
        ' Clear the list box
        .lstSemuaData.Clear

        ' Loop through the data and populate the list box
        For Each cell In ws.Range("A2:E" & iRow)
            ' Calculate the target date (30 days ago)
            targetDate = DateAdd("d", -30, currentDate)

            ' Check if the date in column 2 is past 30 days
            If IsDate(cell.Cells(1, 2).Value) And cell.Cells(1, 2).Value <= targetDate Then
                ' If it's past 30 days, add the row with a different background color
                .lstSemuaData.AddItem cell.Value
                .lstSemuaData.List(i, 2) = cell.Cells(1, 2).Value ' Display the date
                .lstSemuaData.List(i, 2, 1) = RGB(255, 0, 0) ' Highlight the date in red
            Else
                ' If not, add the row without background color
                .lstSemuaData.AddItem cell.Value
                .lstSemuaData.List(i, 2) = cell.Cells(1, 2).Value ' Display the date
            End If
            i = i + 1
        Next cell

    End With 
End Sub

我有Excel VBA代码的问题。所以,我想做的是从表Semua生成数据,并将其显示在列表框名称lstSemuaData上Semua表我有5列数据,第二列我有日期。如果日期超过第2列中日期后的30天,我想在列表框中突出显示该日期。

sbdsn5lh

sbdsn5lh1#

您可以使用ListView更改前景颜色和粗体。

Option Explicit

Sub Reset()

    Dim ws As Worksheet, ar
    Dim dtTarget As Date, li As ListItem
    Dim iRow As Long, c As Long, r As Long
    
    Set ws = Sheets(1)
    With ws
         iRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' identifying the last row
         ar = .Range("A1:E" & iRow)
    End With
    
    ' 30 days ago
    dtTarget = DateAdd("d", -30, Date)
    
    With frmForm
        .txtNoFile.Value = ""
        .txtTarikhNotis.Value = ""
        .txtUlasan.Value = ""

        .ComboBoxStatus.Clear
        .ComboBoxStatus.AddItem "Selesai"
        .ComboBoxStatus.AddItem "Belum Selesai"
        
        ' ListView
        With .lstSemuaData
            .ListItems.Clear
            .ColumnHeaders.Clear
            .View = lvwReport
            .FullRowSelect = True
            For c = 1 To 5
                .ColumnHeaders.Add , , ar(1, c), 90
            Next
       
            ' Loop through the data and populate the listview
            For r = 2 To UBound(ar)
                If IsDate(ar(r, 2)) Then
                    Set li = .ListItems.Add(, , ar(r, 1))
                    For c = 2 To 5
                        li.SubItems(c - 1) = ar(r, c) ' Display the data
                    Next
                    ' check date
                    If ar(r, 2) <= dtTarget Then
                       li.ListSubItems(1).Bold = True
                       li.ListSubItems(1).ForeColor = RGB(255, 0, 0)
                    End If
                End If
            Next
        End With
    End With

End Sub

相关问题