+ Reply to Thread
Results 1 to 4 of 4

Find first and last instance

  1. #1
    Gixxer_J_97
    Guest

    Find first and last instance

    Hi all,

    I have a column containing Month names. Each month is grouped together. IE:
    March
    March
    April
    April
    April
    May
    May
    May
    May
    etc etc

    how do i find the row of the first instance of a certain month, and also the
    row of the last instance of the same month


    tia

    J

  2. #2
    Tom Ogilvy
    Guest

    Re: Find first and last instance

    Sub EFGH()
    Dim rng As Range, rng1 As Range
    Dim sAddr As String
    Set rng = Columns(1).Find("April")
    sAddr = rng.Address
    If Not rng Is Nothing Then
    Do
    Set rng1 = rng
    Set rng = Columns(1).FindNext(rng)
    Loop While rng.Address <> sAddr
    Range(rng, rng1).Select
    End If

    End Sub


    --
    Regards,
    Tom Ogilvy


    "Gixxer_J_97" <[email protected]> wrote in message
    news:[email protected]...
    > Hi all,
    >
    > I have a column containing Month names. Each month is grouped together.

    IE:
    > March
    > March
    > April
    > April
    > April
    > May
    > May
    > May
    > May
    > etc etc
    >
    > how do i find the row of the first instance of a certain month, and also

    the
    > row of the last instance of the same month
    >
    >
    > tia
    >
    > J




  3. #3
    sebastienm
    Guest

    RE: Find first and last instance

    Hi,
    you can use the find function and tell it to search within the whole range
    (say rgSrc) as follow:
    - for the First occurence: start search after the last cell of the range
    rgSrc and search in Direction forward (xlNext)
    - for the Last occurence: start search after the first cell of the range
    rgSrc and search in Direction backward (xlPrevious)

    Sub test()
    Dim rgSrc As Range, rgFirst As Range, rgLast As Range

    Set rgSrc = Range("A2:A120")

    Set rgFirst = rgSrc.Find(what:="March",
    after:=rgSrc.Cells(rgSrc.Cells.Count) _
    ,LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlNext)
    Set rgLast = rgSrc.Find(what:="March", after:=rgSrc.Cells(1), _
    LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlPrevious)
    Debug.Print rgFirst.Address, rgLast.Address
    End Sub
    --
    Regards,
    Sébastien
    <http://www.ondemandanalysis.com>


    "Gixxer_J_97" wrote:

    > Hi all,
    >
    > I have a column containing Month names. Each month is grouped together. IE:
    > March
    > March
    > April
    > April
    > April
    > May
    > May
    > May
    > May
    > etc etc
    >
    > how do i find the row of the first instance of a certain month, and also the
    > row of the last instance of the same month
    >
    >
    > tia
    >
    > J


  4. #4
    Gixxer_J_97
    Guest

    Re: Find first and last instance

    Thanks Tom!

    J

    "Tom Ogilvy" wrote:

    > Sub EFGH()
    > Dim rng As Range, rng1 As Range
    > Dim sAddr As String
    > Set rng = Columns(1).Find("April")
    > sAddr = rng.Address
    > If Not rng Is Nothing Then
    > Do
    > Set rng1 = rng
    > Set rng = Columns(1).FindNext(rng)
    > Loop While rng.Address <> sAddr
    > Range(rng, rng1).Select
    > End If
    >
    > End Sub
    >
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Gixxer_J_97" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi all,
    > >
    > > I have a column containing Month names. Each month is grouped together.

    > IE:
    > > March
    > > March
    > > April
    > > April
    > > April
    > > May
    > > May
    > > May
    > > May
    > > etc etc
    > >
    > > how do i find the row of the first instance of a certain month, and also

    > the
    > > row of the last instance of the same month
    > >
    > >
    > > tia
    > >
    > > J

    >
    >
    >


+ 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