Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Select row from ComboBox populated from noncontiguous cells

Posted on 2003-03-20
Medium Priority
Last Modified: 2011-09-20
- I have a combobox populated from the values in various noncontiguous named ranges
- When I click on the combobox, I want to select the cell that the combobox ListIndex refers to.
- I am using the following code which fails:
    Dim iTargetNum As Integer
    iTargetNum = cboNames.ListIndex + 1              ' Assigns the combobox selection
    Range("AllNames").Cells(iTargetAcctNum).Select   ' "AllNames" is the named range that consists of
                                                     '  various smaller named ranges

- If the first contiguous range is A1:A3, and the second contiguous range is A6:A8, and I select the fourth item in the combobox, cell A4 is selected rather than A6.
Question by:paulmar
  • 2

Accepted Solution

dmang earned 500 total points
ID: 8174191
Interesting problem..try this to find the cell based on your variables noted above..

Sub FindCellInMulitipleAreas()

    Dim a As Integer
    Dim intCellCount As Integer
    Dim iTargetnum As Integer
    iTargetNum = cbonames.listindex + 1
    If Selection.Areas.Count > 1 Then
        For a = 1 To Selection.Areas.Count
            If Not iTargetnum > intCellCount + Selection.Areas(a).Cells.Count Then
               msgbox Selection.Areas(a).Cells(iTargetNum - intCellCount).Value
            End If
            intCellCount = intCellCount + Selection.Areas(a).Cells.Count
        Next a
    End If

End Sub

Author Comment

ID: 8178573

Your code fails because Selection.Areas.Count = 1 whereas in my case it should be picking up a value of 6


Author Comment

ID: 8180088
Your answer closer than I thought.  Selection wasn't the object I required, it was the range.  Some minor modifications to your code were necessary, but the following works beautifully:

Sub FindCellInMulitipleAreas()
    Dim i As Integer                    ' count areas in range
    Dim iTotalCount As Integer      ' aggregate total of cell count
    Dim iTarget As Integer              ' index of combobox selection
    Dim rngBig As Range                 ' range of noncontiguous cells
    Dim iCellCount As Integer           ' no. of cells in current area
    Dim iTemp As Integer                ' preserve total prev. area's cellcount
    Set rngBig = Range("AllRanges")
    iTarget = cboAccounts.ListIndex + 1
    With rngBig
        If .Areas.Count > 1 Then
            For i = 1 To .Areas.Count
                ' Each area has an empty cell: exclude this
                iCellCount = .Areas(i).Cells.Count - 1
                ' Preserve total count up to previous area
                iTemp = iTotalCount
                ' If target cell is in the current area, select it
                If iTarget > iTemp And iTarget <= iTotalCount + iCellCount Then
                   .Areas(i).Cells(iTarget - iTotalCount).Select
                   Exit For
                End If
                iTotalCount = iTotalCount + iCellCount
            Next i
        End If
    End With
End Sub

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question