0

以下の手順では、コンボボックスに a6 から a12 までの値を入力します。しかし、範囲が動的になることに気付きました。データに応じて、3 つのセルまたは 1000 のセルが存在する場合があります。ダイナミック レンジを反映するように以下のコードを調整するにはどうすればよいですか?

Option Strict On
Option Explicit On

Imports Microsoft.Office.Interop.Excel

Public Class dsbPositionBoard

   Private Sub dsbPositionBoard_Startup() Handles Me.Startup

      'This event runs when the dsbPositionBoard starts up. The procedure
      'checks for the values in column A of the allPositionsAnualized sheet
      'and populates the combobox with those values.

      'Variables for procedure
      Dim WB As Excel._Workbook
      Dim WS As Excel.Worksheet
      Dim rng As Excel.Range
      Dim rngArr As String


      WB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
      WS = DirectCast(WB.Sheets("allPositionsAnnualized"), Excel.Worksheet)
      rng = DirectCast(WS.Range("A6", "A12"), Excel.Range)
      rngArr = String.Empty

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

      rngArr = rngArr.Remove(rngArr.Length - 1, 1)
      cmbSelectPosition.Items.AddRange(rngArr.Split(","c))
      rng = Nothing
      WS = Nothing

   End Sub
4

1 に答える 1