+ Reply to Thread
Results 1 to 4 of 4

Index on a selection

  1. #1
    Registered User
    Join Date
    08-15-2006
    Posts
    2

    Index on a selection

    Hi folks.

    First time here, hope you can help me.

    I'm trying to get a index on a for each next routine but I don't know how to do it. Like the Item method. In the example I want the statement to run on the last cell of the selection.

    Example:

    Sub Example()
    For each cell in selection

    if cell.index = selection.count then
    statement
    endif

    next

    end sub

    Thanks for any help you can give.

    Best regards,
    KiO
    Last edited by [KiO] ASamarcos; 08-15-2006 at 01:54 PM.

  2. #2
    Dave Peterson
    Guest

    Re: Index on a selection

    If it's a single area:

    dim MyLastCell as range

    with selection
    set mylastcell = .cells(.cells.count)
    end with

    No need to loop through all the cell.

    "[KiO] ASamarcos" wrote:
    >
    > Hi folks.
    >
    > First time here, hope you can help me.
    >
    > I'm trying to get a index on a for each next routine but I don't know
    > how to do it. Like the Item method. In the example I want the statement
    > to run on the last cell of the selection.
    >
    > Example:
    >
    > Sub Example()
    > For each cell in selection
    >
    > if cell.index = selection.count then
    > statement
    > endif
    >
    > next
    >
    > end sub
    >
    > Thanks for any help you can give.
    >
    > Best regards,
    > KiO
    >
    > --
    > [KiO] ASamarcos
    > ------------------------------------------------------------------------
    > [KiO] ASamarcos's Profile: http://www.excelforum.com/member.php...o&userid=37554
    > View this thread: http://www.excelforum.com/showthread...hreadid=571874


    --

    Dave Peterson

  3. #3
    Registered User
    Join Date
    08-15-2006
    Posts
    2
    Hi Peterson.

    Thanks for your help, but I actually want a way to see the index of the cell that is being processed by the for each next routine.

    Imagine that Selection.Count gives me 40. 40 cells in the range I selected.

    When the For Each starts I want to know, on each step, where the count is. Is it 1, 2, 37?

    I thought the Cell.Index could give me the answer but returns a error message and I think theres a better way to do that than to put a counter on it.

    Best Regards,
    KiO

  4. #4
    Dave Peterson
    Guest

    Re: Index on a selection

    I don't think that there's a better way to do than to count the cells yourself.

    But you do have a few choices.

    You could do:

    dim myRng as range
    dim myCell as range
    dim iCtr as long

    ictr = 0
    set myrng = range("c5:e12")
    for each mycell in myrng.cells
    ictr = ictr + 1
    msgbox ictr & "--" & mycell.address(0,0)
    next mycell

    to see how the "pattern" that mycell takes.

    =============

    or you could loop through the columns of the range:

    dim myRng as range
    dim myCol as range
    dim myCell as range
    dim iCtr as long

    ictr = 0
    set myrng = range("c5:e12")
    for each mycol in myrng.columns
    for each mycell in mycol.cells
    ictr = ictr + 1
    msgbox ictr & "--" & mycell.address(0,0)
    next mycell
    next mycol

    And see that pattern.

    The same kind of thing to loop through each row.

    "[KiO] ASamarcos" wrote:
    >
    > Hi Peterson.
    >
    > Thanks for your help, but I actually want a way to see the index of the
    > cell that is being processed by the for each next routine.
    >
    > Imagine that Selection.Count gives me 40. 40 cells in the range I
    > selected.
    >
    > When the For Each starts I want to know, on each step, where the count
    > is. Is it 1, 2, 37?
    >
    > I thought the Cell.Index could give me the answer but returns a error
    > message and I think theres a better way to do that than to put a
    > counter on it.
    >
    > Best Regards,
    > KiO
    >
    > --
    > [KiO] ASamarcos
    > ------------------------------------------------------------------------
    > [KiO] ASamarcos's Profile: http://www.excelforum.com/member.php...o&userid=37554
    > View this thread: http://www.excelforum.com/showthread...hreadid=571874


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1