Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Listview(Userform)VBA中的唯一行_Excel_Vba_Userform - Fatal编程技术网

Excel Listview(Userform)VBA中的唯一行

Excel Listview(Userform)VBA中的唯一行,excel,vba,userform,Excel,Vba,Userform,我在excel中有一个表,我希望使用listview控件将记录显示在userform中。我面临的挑战是,我只希望显示5列和唯一的行 我目前掌握的守则如下: Private Sub LoadListView() Dim wksSource As Worksheet Dim rngData, rngCell As Range Dim LstItem As ListItem Dim RowCount, ColCount, i, j As Long Dim CountryCol, ShippingWa

我在excel中有一个表,我希望使用listview控件将记录显示在userform中。我面临的挑战是,我只希望显示5列和唯一的行

我目前掌握的守则如下:

Private Sub LoadListView()

Dim wksSource As Worksheet
Dim rngData, rngCell As Range
Dim LstItem As ListItem
Dim RowCount, ColCount, i, j As Long
Dim CountryCol, ShippingWay, SortCode, FirstException, LastException, Performance_OK_NOK, Container, 
CountSimilar, a As Integer

Set wksSource = Worksheets("Sheet3")
Set rngData = wksSource.Range("A1").CurrentRegion

Me.ListView1.ColumnHeaders.Add Text:="RowNr", Width:=70

For Each rngCell In rngData.Rows(1).Cells
If rngCell = "service_def_code" Or rngCell = "package_sort" Or rngCell = "ship_to_country_id" Or 
    rngCell = "first_tracking_exception_message" _
    Or rngCell = "last_tracking_exception_message" Then
       Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=80

End If

Next rngCell

RowCount = rngData.Rows.Count
ColCount = rngData.Columns.Count

For i = 1 To ColCount
If wksSource.Cells(1, i) = "ship_to_country_id" Then
   CountryCol = i
ElseIf wksSource.Cells(1, i) = "service_def_code" Then
   ShippingWay = i
ElseIf wksSource.Cells(1, i) = "package_sort" Then
   SortCode = i
ElseIf wksSource.Cells(1, i) = "first_tracking_exception_message" Then
   FirstException = i
ElseIf wksSource.Cells(1, i) = "last_tracking_exception_message" Then
   LastException = i
ElseIf wksSource.Cells(1, i) = "performance_result" Then
   Performance_OK_NOK = i

End If

Next i

j = 1

For i = 2 To RowCount
If wksSource.Cells(i, Performance_OK_NOK) = "NOK" then
  Set LstItem = Me.ListView1.ListItems.Add(Text:=j)
      LstItem.ListSubItems.Add Text:=rngData(i, CountryCol)
      LstItem.ListSubItems.Add Text:=rngData(i, ShippingWay)
      LstItem.ListSubItems.Add Text:=rngData(i, SortCode)
      LstItem.ListSubItems.Add Text:=rngData(i, FirstException)
      LstItem.ListSubItems.Add Text:=rngData(i, LastException)
      j = j + 1

end if

next i

end sub

所以我想做的是只显示唯一的行,子项表示一行。我检查并寻找了一个解决方案,但没有找到一个我能理解的。有人能帮忙吗?

你可以用字典。为每一行创建一个包含五列值的键。如果它不在字典中,请将其添加到字典中,然后将其添加到listview中

下面的示例从列a、b创建一个键。调整它,以便根据五列创建键。下面我只得到一次b2,即使它在表a、b中出现两次

如果EXCEL支持UNIQUE函数,则不需要VBA。

您可以将记录复制到临时工作表中,然后使用。然后,此表可以是listview的行源。但如果没有关于您所拥有记录的任何进一步信息,您将很难得到帮助,至少在我看来是这样。我认为您需要从单元格值中删除空白。正如您上面所说,但是没有关于记录的任何进一步信息。。。。我想你能做到。是的,对:-我们需要一个
Public Sub sAddToList()

  'REQUIRES MICROSOFT SCRIPTING RUNTIME LIB, (Add using Tools->References from the VB menu)
  
  Dim d As Dictionary
  Dim rowKey As String
  Dim i As Integer
  
  Set d = New Dictionary

  For i = 1 To 4
    rowKey = CStr(Sheet1.Cells(i, 1).Value) + CStr(Sheet1.Cells(i, 2).Value)
    If Not d.Exists(rowKey) Then
      d.Add rowKey, rowKey
      'add to your list view
    End If
  Next

End Sub