2016-06-26 212 views
0

我有代碼查找「!UKINADMISSIBLE」列中的「M」列。那麼它將在列表框(listbox1)中顯示所有選中的行具有「!UKINADMISSIBLE」。它工作正常,但如果我從工作表中刪除所有「!UKINADMISSIBLE」它會給我這個錯誤(無法設置列表屬性,無效屬性值)在這行代碼--->Me.ListBox1.List = arrLstBox( )--->錯誤 任何人都可以幫我修復它。無法設置列表屬性。無效的屬性值錯誤

Private Sub btnIUK_Click() 

Dim arrLstBox() 
Dim rng, FoundCell, tmpCell As Range 
Dim i, j, numRows, lastColumn, lastRow As Long 
Dim FirstAddress, searchFor, colWidth As String 

Set rng = ActiveSheet.UsedRange 
numRow = 0 

With rng 

    lastRow = .Rows.Count 
    lastColumn = .Columns.Count 

End With 

Me.ListBox1.ColumnCount = lastColumn 
Me.ListBox1.ColumnWidths = "60;70;190;40;90;90;70;80;50;60;90;120;5" 


    Set FoundCell = rng.Find(what:="!UKINADMISSIBLE", LookIn:=xlValues, lookat:=xlWhole) 

    If Not FoundCell Is Nothing Then _ 
    FirstAddress = FoundCell.Address 

    Do Until FoundCell Is Nothing 

     Set FoundCell = rng.FindNext(after:=FoundCell) 

     If FoundCell.Address = FirstAddress Then 
      numRow = numRow + 1 
      Exit Do 
     ElseIf FoundCell.Row <> rng.FindNext(after:=FoundCell).Row Then 
      numRow = numRow + 1 
     End If 

ReDim arrLstBox(1 To numRow + 1, 1 To lastColumn + 1) 

Loop 

Do Until FoundCell Is Nothing 

    For i = 1 To numRow 
     For j = 1 To lastColumn 

      If Not IsEmpty(Cells(FoundCell.Row, j).Value) Then 

       arrLstBox(i, j) = Cells(FoundCell.Row, j).Value 

      End If 


     Next j 

     Set FoundCell = rng.FindNext(after:=FoundCell) 

     If FoundCell.Address = FirstAddress Then _ 
      Exit For 

    Next i 

    If FoundCell.Address = FirstAddress Then _ 
      Exit Do 

Loop 

Me.ListBox1.List = arrLstBox()----->ERROR 

lastRow = ListBox1.ListCount 
MsgBox "Records Found = " & lastRow, vb, "Inadmissibles On UK Sectors" 

End Subode here 

回答

0

問題是你的數組是空的。在你的代碼中,你應該測試這個,如果是這樣的話,只需ClearListBox

你的代碼似乎有點低效。例如,您正在整個文檔中搜索您只希望在列M中找到的項目,然後運行搜索兩次。爲什麼不在M列中搜索一次,並將'hit'行存儲在一個變量中?然後你可以簡單地用這些行填充ListBox數組。您也可以考慮在Userform_Initialize事件中只調整一次ListBox列。

你也應該知道你的大部分聲明是Variants。你必須明確地聲明每個變量,像這樣Dim a As Integer, b As Integer

這樣做的一個框架代碼可能看起來像下面的東西:

Option Explicit 

Private Sub btnIUK_Click() 
    Dim v As Variant 
    Dim i As Long 
    Dim j As Long 
    Dim hits As Collection 
    Dim hit As Variant 
    Dim arrItems() As Variant 

    'Read values into an array 
    v = ThisWorkbook.Worksheets("Sheet1").UsedRange.Value2 

    'Find the target values 
    Set hits = New Collection 
    For i = 1 To UBound(v, 1) 
     If v(i, 13) = "!UKINADMISSIBLE" Then hits.Add i 
    Next 

    'Populate the listbox array with the hit items 
    If hits.Count > 0 Then 
     ReDim arrItems(1 To hits.Count, 1 To UBound(v, 2)) 
     i = 1 
     For Each hit In hits 
      For j = 1 To 13 
       arrItems(i, j) = v(hit, j) 
      Next 
      i = i + 1 
     Next 
     Me.ListBox1.List = arrItems 
    Else 
     'There are not hits so clear the listbox 
     Me.ListBox1.Clear 
    End If 

End Sub 

Private Sub UserForm_Initialize() 
    With Me.ListBox1 
     .ColumnCount = 13 
     .ColumnWidths = "60;70;190;40;90;90;70;80;50;60;90;120;5" 
    End With 

End Sub 
+0

感謝負載,真的很感激。再次感謝:) –

+0

謝謝你Ambie,真的很酷 –

0

你是幾乎沒有。 只需添加元素到列表框中,如果有在M列的至少一個細胞與搜索值

此外,如果在列始終加上數據A至M,那麼你可以既避免計數列和列表框中設置轉移到UserForm_Initialize子。

像這樣:

Private Sub btnIUK_Click() 

Dim arrLstBox() As Variant 
Dim foundCell As Range 
Dim i As Long, j As Long, nCells As Long 
Dim firstAddress As String 

With ThisWorkbook.Worksheets("MySheet") '<--| always specify the worksheet name 
    With .Range("M", .Cells(.Rows.Count, 13).End(xlUp)) '<--| consider column M cells down to its last non empty one 
     nCells = WorksheetFunction.CountIf(.Cells, "!UKINADMISSIBLE") '<--| count searched value occurrences in column M 
     If nCells > 0 then '<--| If there's at least one occurrence ... 
      ReDim arrLstBox(1 To nCells, 1 To 13) '<--| ... ReDim your array... 
      Set foundCell = .Find(what:="!UKINADMISSIBLE", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) '<--| ...find first occurrence (it's there for sure!) 
      firstAddress = foundCell.Address '<--| ...and store first occurrence address 
      Do '<--| first loop is granted! 
       i = i + 1 '<--| update array row index 
       For j = 1 To 13 '<--| fill array row 
        arrLstBox(i, j) = foundCell.Offset(,-13 + j) '<--| use Offset from found cell to sta in its row and loop through columns 1 To 13 
       Next 
       Set foundCell = .FindNext (foundCell) '<--| look for subsequent occurrence 
      Loop While firstAddress <> foundCell.Address '<--| subsequent loops are made till Find() wraps back to the first one 
      Me.ListBox1.List = arrLstBox 
     End If '<--| fill listbox 
    End With 
End With 
MsgBox "Records Found = " & nCells, vb, "Inadmissibles On UK Sectors" 

End Sub 


Private Sub UserForm_Initialize() 
    With Me.ListBox1 
     .ColumnCount = 13 
     .ColumnWidths = "60;70;190;40;90;90;70;80;50;60;90;120;5" 
    End With 
End Sub 
+0

哦,是的,我搞砸了幾個代碼行。謝謝user3598756您的時間。非常感謝。 –