2016-07-27 2 views
1

개선이 필요한 작업 매크로가 있습니다. 매크로의 일부가 Excel 사용자 정의 폼의 목록 상자를 채 웁니다. 코드는 아래와 같다 :Excel 목록 상자의 특정 행을 기본값으로 설정

Dim objExcelApp    As Object 
Dim objExcelWorkBook  As Object 
Dim objExcelWorksheet  As Object 
Dim varTest     As Variant 

lngLastLitRow = objExcelApp.Sheets(1).UsedRange.Rows.Count 
Dim strArray() As Variant 
ReDim strArray(lngFilteredRowCount - 1, 2) 

'Load strArray 
With objExcelWorkBook.Sheets(1) 
    'The ListBox dimention starts with (0), so set lngListBoxRow 
    lngListBoxRow = 0 

    'Loop through all the rows in rngLiturgies 
    For Each rngCurrentLitRow In rngLiturgies.Rows 
     'Populate lngExcelRow 
     lngExcelRow = rngCurrentLitRow.Row 
     'Select only the rows that are NOT filtered 
     If rngCurrentLitRow.EntireRow.Hidden = False Then 

     'Fill the array row (lngListBoxRow) with Excel data (lngExcelRow) 
     'The Array's column 0 equates to Excel's column 1 
     strArray(lngListBoxRow, 0) = objExcelWorkBook.Sheets(1).Cells(lngExcelRow, 1) 
     strArray(lngListBoxRow, 1) = objExcelWorkBook.Sheets(1).Cells(lngExcelRow, 2) 
     strArray(lngListBoxRow, 2) = objExcelWorkBook.Sheets(1).Cells(lngExcelRow, 3) 

     'Increment lngListBoxRow "manually" since it doesn't correspond to 
     'the Excel row in the FOR loop 
     lngListBoxRow = lngListBoxRow + 1 

     Else 
     End If 

    Next rngCurrentLitRow 

End With 

'Load ListBoxLit with strArray 
With ListBoxLit 
    .ColumnCount = 3 
    .List = strArray 
End With 

인핸스 같이, I가리스트 박스 내의 행 기본하려는마다 다른 형태의 전계 변화 값. 해당 필드 변경을 기반으로 배열의 요소에 해당하는 세 변수의 값을 알 수 있습니다. 이 세 가지 값을 사용할 수있는 경우 목록 상자에서 행을 선택하려면 어떻게해야합니까? 고마워요.

+2

'ListBox.Items'의 각 항목에는'.Selected' 속성 (부울 값)이 있습니다. –

+0

안녕하세요 @DavidZemens, 답장을 보내 주셔서 감사합니다. 어떻게 세 요소의 값을 알고 있는지 ListBoxLit에서 행을 "선택"하는 방법은 무엇입니까? ListBoxLit.selected = ...... – user3138025

답변

1

MSForms.ListBox 클래스의 Selected 메서드는 목록 내의 위치 인 인덱스이라는 단일 인수를 취합니다. 세 개의 값이있는 경우

Call SelectItemInListBox("some value", ListBoxLit) 

방금 ​​호출 할 수 있습니다

Sub SelectItemInListBox(val$, lBox as MSForms.ListBox) 
    Dim i as Long 
    For i = 0 to lBox.ListCount - 1 
     If LBox.List(i) = val Then LBox.Selected(i) = True 
    Next 
End Sub 

당신은 당신이 선택하려는값 및 목록 상자 객체 자체, 예를 들어,이 같은 함수를 호출하고 전달할 수 있습니다 이 기능을 세 번.

+0

과 같은 것이 완벽하게 작동했습니다. 감사합니다 @DavidZemens – user3138025

관련 문제