2016-06-26 1 views
0

시트의 "M"열에 "! UKINADMISSIBLE"에 대한 조회 코드가 있습니다. 목록 상자 (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 열에서만 한 번만 검색하고 '히트'행을 변수에 저장하는 것이 좋을까요? 그런 다음 해당 행으로 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에서 적어도 하나 개의 셀이 있는지 그냥 목록 상자에 요소를 추가

데이터는 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. 잘 부탁드립니다. –