2017-06-23 61 views
-3

我基本上想知道什麼是最好的方式來比較工作表中的項目與列表框,基本上,列表框中的項目需要在工作表上找到,如果他們是沒有發現他們需要去下一張免費行的表底部,最好的方式來找到匹配的工作表相比,列表框vba

這應該不是太難,但我試過使用和if語句,但發現它必須搜索所有行上的片所有這使得它laggy,耗時,有時反應遲鈍列表框內部的行,

我想使用.find方法,但不想浪費我的時間,

更新的代碼:

`

  For i = 0 To Me.ListBox1.ListCount - 1 

      field1 = Me.ListBox1.List(i) 
      field2 = Me.ListBox1.List(i, 1) 
      field3 = Me.ListBox1.List(i, 2) 


      field2ammend = Right(field2, Len(field2) - 7) 


    For Each rCell In rRng.Cells 






     If rCell.Value = field1 Then 

      comp = field3 
      name = field2ammend 

      Sheets("Hair").Range("E" & rCell.Row) = comp 
      Sheets("Hair").Range("F" & rCell.Row) = name 

      Range("A" & rCell.Row & ":H" & rCell.Row).Interior.ColorIndex = 24 

      countgood = countgood + 1 




     Else 




      ListBox2.AddItem (field2) 

      'bal = bal + 1 
      'Sheets("Hair").Range("B" & lastrows) = field1 
      'Sheets("Hair").Range("E" & lastrows) = comp 
      'Sheets("Hair").Range("F" & lastrows) = name 
      'Range("A" & lastrows & ":H" & lastrows).Interior.ColorIndex = 24 
      'lastrows = lastrows + 1 
      'countbad = countbad + 1 
     End If 

下一頁RCELL 接下來我

`

任何建議,

謝謝

+0

用你到目前爲止試過的代碼更新你的問題。給我們一個關於你的數據大小的想法。 '.find'方法是一個很好的開始,你實際上*應該*浪費一些時間。 –

+0

沒有人*最好的方式*。許多因素會影響你可以採取的所有不同方法(列表中的記錄數量,列表中包含的數據類型等)的效率。原型和測試不是浪費你的時間。您需要嘗試找到最適合您的解決方案。 –

+0

你好@DavidG我已經更新了我嘗試過的代碼,並且我正在處理列表框中的大約40-50項,以便與大約5000行數據進行比較,感謝所有幫助! –

回答

0

我不知道原因,但是,doe這有幫助嗎?

Sub Weiter_Click() 
Dim i As Integer 
Dim varTemp As Variant 

    varTemp = ListBox1.List '<-- for listboxes with one column. you have to edit this to pass your code 

    For i = LBound(varTemp) To UBound(varTemp) 
     If Columns("A:A").Find(varTemp(i, 0)) Is Nothing Then '<--- there is the column you're looking in 
      Cells(1, 1).End(xlDown).Offset(1, 0).Value = varTemp(i, 0) 
     Else 
      Columns("A:A").Find(varTemp(i, 0)).Interior.ColorIndex = 3 '<--- 3 is red, idk what color you want 
     End If 
    Next 
End Sub 
+0

你好@Pearli這是我需要的東西,但我想留下找到的東西,給他們一種顏色,其餘的東西沒有被發現添加到工作表底部,謝謝你的幫助。 –

+0

嗨@AshleyDavies,我編輯了我的答案。已經在列a中的項目的背景顏色發生了變化。 – Pearli

+0

謝謝你,現在就來測試一下,謝謝你的幫助! –

相關問題