2014-04-14 2 views
0

목록 상자에서 선택된 항목의 행을 어떻게 찾을 수 있습니까?목록 상자에서 선택된 항목의 행 번호를 찾으십시오.

지금 내가 목록 상자에 발견 된 항목을 모두 입력하는 sub을 가지고

Sub findnext() 
Dim Name As String 
Dim f As Range 
Dim ws As Worksheet 
Dim s As Integer 
Dim findnext As Range 

Set ws = ThisWorkbook.Worksheets("Master") 
Name = surname.Value 
ListBox1.Clear 
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues) 
Set findnext = f 

Do 
Debug.Print findnext.Address 
Set findnext = Range("A:A").findnext(findnext) 

ListBox1.AddItem findnext.Value 
ListBox1.List(ListBox1.ListCount - 1, 1) = ws.Cells(findnext.Row, xFirstName).Value 
ListBox1.List(ListBox1.ListCount - 1, 2) = ws.Cells(findnext.Row, xTitle).Value 
ListBox1.List(ListBox1.ListCount - 1, 3) = ws.Cells(findnext.Row, xProgramAreas).Value 
ListBox1.List(ListBox1.ListCount - 1, 4) = ws.Cells(findnext.Row, xEmail).Value 
ListBox1.List(ListBox1.ListCount - 1, 5) = ws.Cells(findnext.Row, xStakeholder).Value 
ListBox1.List(ListBox1.ListCount - 1, 6) = ws.Cells(findnext.Row, xofficephone).Value 
ListBox1.List(ListBox1.ListCount - 1, 7) = ws.Cells(findnext.Row, xcellphone).Value 
'ListBox1.List(ListBox1.ListCount - 1, 8) = ws.Cells(findnext.Row, xFirstName).Value 
Loop While findnext.Address <> f.Address 

End Sub 

아래를 참조하고 사용자가 목록 상자에서 항목을 선택하면, 다음은에 정보를 채우는 것 사용자 정의 폼 나는 목록 상자에서 선택한 항목의 행 번호를 확인하는 방법을 ListBox1_click()에서 파악하려는

Sub ListBox1_Click() 

    surname.Value = ListBox1.List(ListBox1.ListIndex, 0) 
    firstname.Value = ListBox1.List(ListBox1.ListIndex, 1) 
    tod.Value = ListBox1.List(ListBox1.ListIndex, 2) 
    program.Value = ListBox1.List(ListBox1.ListIndex, 3) 
    email.Value = ListBox1.List(ListBox1.ListIndex, 4) 
    SetCheckBoxes ListBox1.List(ListBox1.ListIndex, 5)  '<<<< added 
    officenumber.Value = ListBox1.List(ListBox1.ListIndex, 6) 
    cellnumber.Value = ListBox1.List(ListBox1.ListIndex, 7)  

End Sub 

의 텍스트 상자. 일단 내가 이것을 알아 내면 update sub 코드를 작성하여 선택한 항목의 행 번호를 찾고 텍스트 상자에 정보를 다시 입력하고 행 정보를 업데이트합니다.

은 내가 find을 숨겨진 워크 시트의 행 번호를 저장하는 생각 ..하지만 난 희망 listbox

에서 선택되어 있는지와 found의 행 번호를 연결하는 방법을 모른다 .. 이게 의미가있어, 그렇지 않으면 알려줘!

답변

1

필자가 아는 한, 다중 선택을 할 수 있으므로 모든 행을 ListBox에 루프해야합니다.

For r = 0 To ListBox1.ListCount - 1 
    If ListBox1.Selected(r) Then 
     Debug.Print "You selected row #" & r + 1 
    End If 
Next 
+0

그래서'debug.print' 대신에 다음과 같이 썼습니다 :'worksheets ("hs"). cells (1,1) = r' 그리고 당신에게 제공 한 다음 코드를 사용했습니다. 'hs'에서 1의 값. 'hs'는 선택된 항목의 행 번호를 저장하고자하는 숨겨진 시트입니다. 문제는, 그것은 나에게 목록 상자에서 행 번호를 주며, 행 번호는 Excel 스프레드 시트에서 찾을 수있다. 예를 들어,'apple'이라는 항목이 있고'search'를하고 apple을 찾으면 apple에 관한 모든 정보를'listbox'에 입력합니다. 내 질문에 어떻게'행 상자를'사과'목록 상자에서 선택했을 때 찾을 수 있습니까? 말이 돼...? – Doolie1106

+0

"목록 상자에서 선택한 항목의 행 번호를 어떻게 결정할 수 있습니까?"라는 질문을하셨습니까? ListBox에 포함 된 워크 시트 셀을 클릭 할 때 저장/찾으려고합니까? – PatricK

+0

예, 목록 상자에서 클릭하면 워크 시트에있는 항목의 행 번호를 찾으려고합니다. 리스트 박스는'find' 함수 다음에 채워집니다. 그래서리스트 박스에서 클릭 한 후에'found' 아이템의 행 번호를 찾고 싶습니다. – Doolie1106

2

나는이 문제를 갈 수있는 방법을 발견했습니다 - 나는 항목이 검색 됐어요 때 foundrow#를 저장하는 여분의 listcolumn을했습니다. 그 다음 행을 입력 할 수있는 userform 내에서 다른 텍스트 상자를 만들었습니다. #

따라서 findnext() 하위에 다음 줄을 추가하고 목록 상자에 다른 줄을 추가했습니다. `listbox1.list (listbox1.listcount-1,8) = storerownumber를

listbox1_click() 부 내에 난 다음 줄에 추가 한

: rownumber.value = listbox1.list(listbox1.listindex,8)

를 다음 줄 새로운 sub update_click() 만들었고, 추가 :

Dim r As Long 
Dim update As Range 
Dim ws As Worksheet 

Set ws = Worksheets("Master") 
rownumber.Value = ListBox1.List(ListBox1.ListIndex, 8) 
r = rownumber 

ws.Cells(r, xLastName).Value = surname.Value 
ws.Cells(r, xFirstName).Value = firstname.Value 
ws.Cells(r, xTitle).Value = tod.Value 
ws.Cells(r, xProgramAreas).Value = program.Value 
ws.Cells(r, xEmail).Value = email.Value 
ws.Cells(r, xStakeholder).Value = GetCheckBoxes 
ws.Cells(r, xofficephone).Value = officenumber.Value 
ws.Cells(r, xcellphone).Value = cellnumber.Value 

end sub 

그리고 제대로 작동합니다.

관련 문제