【问题标题】:Access values in array and display on combobox访问数组中的值并显示在组合框中
【发布时间】:2013-08-21 03:00:25
【问题描述】:

这远远超出了我的技能范围,坦率地说,我从来没有做过这样的事情,也不知道是否可行。下面的过程根据 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 上的值,然后在第一个过程的组合框中显示该值。基本上,不是我下拉并从数组中选择项目,而是函数会为我搜索数组并选择该项目。

如果我不是很清楚,我可以澄清或发布屏幕截图。

【问题讨论】:

  • 我建议您发布屏幕截图和一个简单的示例,就像您在其他问题中一样。正如您所看到的,一旦清楚地传达了想法,帮助就会很快到来。
  • @varocarbas,我会准备一些截图并发布它们。谢谢

标签: vb.net excel


【解决方案1】:

这将是使用 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

调用该函数(添加一些错误处理/空引用检查),参数为单元格 B37 的值(或任何单元格值作为字符串)。

【讨论】:

  • 通过一些小的调整,我能够让它工作。谢谢
猜你喜欢
  • 1970-01-01
  • 2010-12-11
  • 2016-08-04
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多