2013-08-21 72 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的值作爲搜索參數來查詢列表。

聲明在類的榜首(外的任何方法)

Private _myList As New List(Of String) 

將此代碼添加到您的第一個方法

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

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

xlRng = Nothing 
xlWS = Nothing 

現在大致如下增加一個功能:

Private Function QueryValues(ByVal myParameter as String) As String 
    Dim results = From result In _myList Where result = myParameter Select result Distinct 
    Return results(0) 
End Function 

用你的參數調用該函數(雖然增加了一些錯誤處理/空引用檢查) eter是單元格B37的值(或任何單元格值作爲字符串)。

+0

隨着一些小調整,我能夠得到它的工作。謝謝 –