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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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