+ Reply to Thread
Results 1 to 7 of 7

Thread: can i address the cells selected when holding the CTRL key?

  1. #1
    Registered User
    Join Date
    03-30-2011
    Location
    NY, NY
    MS-Off Ver
    Excel 2003
    Posts
    7

    can i address the cells selected when holding the CTRL key?

    can i address the cells selected when holding the CTRL key?

    folks, here's the background:

    1. say i have a column of data
    2. i select 5 cells (out of 50) by holding down the CTRL key
    3. i'd like to pass the cell selections to an array
    4. the array would then be passed to a procedure when i hit a button, and the procedure would iterate through the array to perform some calculations on the cell values

    i tried to record a macro, but it records the 'range' of each of the selected cells and doesn't expose where excel is 'holding' the multiple selections. i am guessing there is range, select and selection to be used here, but i have no clue how to do it.

    yes, i'm new, so maybe this is obvious to everyone -- but i can't figure it out.

    thanks for any feedback

    ron

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: can i address the cells selected when holding the CTRL key?

    Something like this maybe:

    Sub test()
    
    Dim rngLoopRange As Range
    
    For Each rngLoopRange In Selection
        rngLoopRange = rngLoopRange + 1
    Next rngLoopRange
    
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,151

    Re: can i address the cells selected when holding the CTRL key?

    or, if you really want the cell values in an array:

    Sub test()
    Dim arCells As Variant
    Dim cel As Range
    Dim i As Long
    i = 1
    ReDim arCells(1 To Selection.Count)
    ' fill the array
    For Each cel In Selection
        arCells(i) = cel.Value
        i = i + 1
    Next cel
    
    ' do something with the array
    For j = 1 To UBound(arCells)
        Debug.Print arCells(j)
    Next j
    
    End Sub
    cheers,

  4. #4
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: can i address the cells selected when holding the CTRL key?

    @teylyn

    I was going to recommend skipping the cycle through the selected range, and just do something like...
        Dim arCells As Variant
    
        arCells = Selection.Value

    ...but after testing it, I found that this only works for adjacent cells. I cannot seem to get it to work for a non-adjacent selection without doing as you have done, and cycling through each cell in the selected range.

    But, in my mind, there must be a better way than looping through each value. I've noticed that you tend to offer this kind of advice, feeding a range into an array to work with rather than working directly with the range. This is obviously much faster. What is your experience with nonadjacent ranges? Is looping the only way?

  5. #5
    Registered User
    Join Date
    03-30-2011
    Location
    NY, NY
    MS-Off Ver
    Excel 2003
    Posts
    7

    Smile Re: can i address the cells selected when holding the CTRL key?

    guys -- DOMSKI, TEYLYN, WHIZBANG -- thanks

    this stuff, once you see it, always turns out 'understated' in it's simplicity.
    yet, believe me, i spent 3 days trying to find out what ballpark i was in.

    i assumed if i could select multiple cells, they had to be held 'somewhere',
    but, obviously,
    who would think to put it out in the open under something as 'obscure' as 'selection'

    (see, i was a physicist once, so i just had to phantasize, it's only the engineers that have to make stuff work -- so this making stuff work part is new to me)

    thanks again - makes a lot of sense now
    ron

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: can i address the cells selected when holding the CTRL key?

    It's only obvious if you know Most of us have spent years doing things the hard way before someone pointed out the error of our ways.

    For info it would also be possible to check that the selection is within a certain range so as you're not jumping through selections that you don't want to. Another lesson maybe though...

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  7. #7
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,151

    Re: can i address the cells selected when holding the CTRL key?

    @ron, glad you found something helpful.

    @whizbang, I got my insights from this thread. If shg says you need to loop, then you need to loop.

    http://www.excelforum.com/excel-prog...-an-array.html

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0