2013-08-21 5 views
0

이것은 솔직히 말해서 내 기술 세트를 뛰어 넘었습니다. 나는 이런 식으로 한 번도 해 본 적이 없으며 가능한지 모르겠습니다. 아래 절차에서는 B6 열의 값을 기반으로 배열을 만듭니다.어레이의 값을 액세스하고 콤보 상자에 표시

Private Sub dsbPositionBoard_Startup() Handles Me.Startup 

    'This event runs when the dsbPositionBoard starts. The procedure 
    'checks for the values in column A of the allPositionsAnualized sheet 
    'and populates the combobox with those values. If there are no values the box 
    'is disabled. 


    Dim xlRng As Excel.Range 
    Dim strRngArr As String 
    Dim strChkRange As String 

    Try 

     xlWB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook) 
     xlWS = DirectCast(xlWB.Sheets("allPositionsAnnualized"), Excel.Worksheet) 
     xlRng = DirectCast(xlWS.Range("B6", xlWS.Range("B6").End(Excel.XlDirection.xlDown)), Excel.Range) 
     strRngArr = String.Empty 
     strChkRange = CStr(xlWS.Range("B6").Value) 

     If (String.IsNullOrEmpty(strChkRange)) Then 

      cmbSelectPosition.Enabled = False 

     Else 


      'Build a string array delimited by commas 
      For i As Integer = 1 To xlRng.Rows.Count 
       Dim xlRngCell As Excel.Range = DirectCast(xlRng.Rows(i), Excel.Range) 
       strRngArr &= DirectCast(xlRngCell.Value.ToString, String) & "," 

      Next 

      strRngArr = strRngArr.Remove(strRngArr.Length - 1, 1) 
      cmbSelectPosition.Items.AddRange(strRngArr.Split(","c)) 
      xlRng = Nothing 
      xlWS = Nothing 

     End If 

    Catch ex As Exception 

     MsgBox("There no positions available to select", CType(vbOKOnly, MsgBoxStyle), "Empty Selection") 

    End Try 

End Sub 

이제 아래 함수는 셀 범위의 값을 선택 헬퍼 세포 (B37)에 전달하고 대응하는 시트를 선택하는 데 사용된다. 이 함수가 도우미 셀에 전달하는 값은 위 배열의 값과 같습니다.

Private Function MoveBtwSheets(range As String) As String 

    'This function is used to toggle between the position board 
    'and the employee board. The function is utilized to select 
    'the employees listed in the position board, click on the radio button 
    ' and open that employees information in the employee board 

    '@parameter range: Selects the cell with the employee name 

    Dim xlCalc As Excel.Worksheet 


    strMessage = "This employee does not exist. Please verify the employee name" 
    strCaption = "Selection Error" 
    msgBoxType = MessageBoxIcon.Error 
    msgBoxBtns = MessageBoxButtons.OK 

    xlWB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook) 

    xlCalc = CType(xlWB.Worksheets("calculationSheets"), Excel.Worksheet) 
    xlWSEE = CType(xlWB.Worksheets("employeeBoard"), Excel.Worksheet) 
    xlWSPOS = CType(xlWB.Worksheets("positionBoard"), Excel.Worksheet) 


    Application.ScreenUpdating = False 

    Try 

     xlCalc.Range("B36").Value = xlWSPOS.Range(range).Value 

     With xlWSEE 

      .Select() 
      .Range("E37").Select() 


     End With 

     Application.ScreenUpdating = True 

    Catch ex As Exception 

     MessageBox.Show(strMessage, strCaption, msgBoxBtns, msgBoxType) 

    End Try 


    Return "" 


End Function 

그래서 내가 기능을 추가하고 싶었던 것을 B37의 값에 대한 내 배열을 검색 한 후 첫 번째 절차에 콤보 상자에 그 값을 표시하는 방법입니다. 기본적으로 배열 대신 항목을 선택하는 대신 함수가 배열을 검색하여 해당 항목을 선택합니다.

매우 명확하지 않은 경우 스크린 샷을 명확히하거나 게시 할 수 있습니다.

+0

다른 질문과 마찬가지로 스크린 샷과 간단한 예를 게시하는 것이 좋습니다. 보시다시피 아이디어가 명확하게 전달되면 도움이 신속하게 제공됩니다. – varocarbas

+0

@varocarbas, 나는 스크린 샷을 준비하고 게시 할 것이다. 감사합니다 –

+0

우수! 감사. – varocarbas

답변

2

이것은 LINQ를 사용하기에 좋은 시간입니다. 초기 방법 (dsbPositionBoard_Startup())에서 A 열의 각 문자열을 List(Of String)에 추가 할 수 있습니다. 그런 다음 B37 값을 검색 매개 변수로 사용하여 목록을 쿼리 할 수 ​​있습니다.

strRngArr = strRngArr.Remove(strRngArr.Length - 1, 1) 
cmbSelectPosition.Items.AddRange(strRngArr.Split(","c)) 

_myList.Add(strRngArr.Split(","c)) 

xlRng = Nothing 
xlWS = Nothing 

이제 다음과 같은 라인을 따라 함수를 추가

Private _myList As New List(Of String) 

첫 번째 방법이 코드를 추가합니다 (임의의 방법 이외의) 클래스의 상단에있는 목록을 선언 :

해당 함수를 호출하여 (오류 처리/null 참조 검사를 일부 추가합니다) 매개 변수로 eter는 셀 B37의 값 (또는 셀 값을 문자열로 나타냄)입니다.

+0

약간의 미세한 조정을 통해 작동시킬 수있었습니다. 감사 –

관련 문제