0

在此处输入图像描述下面的过程对我来说很好用了一段时间。但是,现在数据发生了一些变化。数据是名称,它们的格式为姓氏、名字。不幸的是,我下面的程序被格式化为用逗号分隔,所以正在做的是姓氏名字。我不想那样做,我想保持原样。我怎样才能改变下面的程序来做到这一点?

    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

我尝试将这两行更改为:

strRngArr &= DirectCast(xlRngCell.Value.ToString, String)

cmbSelectPosition.Items.Add(strRngArr.ToString)

这应该有效,但是我只得到了数组的第一项,并且它被格式化为姓氏,名字姓氏。

4

1 回答 1

1

从您的评论中,我了解到您想要的是替换循环和Combobox填充的行:

For i As Integer = 1 To xlRng.Rows.Count
    cmbSelectPosition.Items.Add(DirectCast(xlRng.Rows(i), Excel.Range).Value)
Next
于 2013-08-21T07:37:27.417 回答