+ Reply to Thread
Results 1 to 5 of 5

Get 1st and last addresses of cells in a range

Hybrid View

  1. #1
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327

    Get 1st and last addresses of cells in a range

    Hi all,

    If I have more than one occurrence of a word (say "Ford") in Col A (say A1, A5, A10, and A14) and I want (with VBA) to return the number of occurrences and the first and last cell addresses (ie 4 times and "A1" and "A14") can anyone point me in the right direction please?

    Regards

    Seamus
    Hope this helps

    Seamus

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    May have over thought these but

    Dim Rng As Range, FndLast As Range
    Dim FndWrd As String
    Dim i As Long, LastRow As Long, WrdCnt As Long
    
    FndWrd = "Ford"
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set Rng = Range("A1:A" & LastRow)
    
    WrdCnt = WorksheetFunction.CountIf(Rng, FndWrd)
    
    MsgBox WrdCnt
    
        For i = 1 To LastRow
            If Cells(i, "A").Value = FndWrd Then
                MsgBox Cells(i, "A").Address(0, 0)
            Exit For
            End If
        Next i
        
    Set FndLast = Rng.Find(FndWrd, After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        
        If Not FndLast Is Nothing Then
            MsgBox FndLast.Address(0, 0)
        End If
    or

    Dim Rng As Range, FndFirst As Range, FndLast As Range
    Dim FndWrd As String
    FndWrd = "Ford"
    Set Rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    MsgBox WorksheetFunction.CountIf(Rng, FndWrd)
    
    If Range("A1").Value = FndWrd Then
         MsgBox Range("A1").Address(0, 0)
    Else
    Set FndFirst = Rng.Find(What:=FndWrd)
            
        If Not FndFirst Is Nothing Then
            MsgBox FndFirst.Address(0, 0)
        End If
    End If
    
    Set FndLast = Rng.Find(FndWrd, After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        
        If Not FndLast Is Nothing Then
            MsgBox FndLast.Address(0, 0)
        End If
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Many thanks for that vbanoob. I also meant to ask in my first question how I would go about deleting the occurrences in between 1st and last (ie A5 and A10).

    Any help would be great?

    Regards

    Seamus

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe.

    Change delete to clearcontents if required

    Dim FndWrd As String
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    FndWrd = "Ford"
    
    For i = LastRow To 1 Step -1
    WrdCnt = WorksheetFunction.CountIf(Range("A1:A" & LastRow), FndWrd)
        If Not WorksheetFunction.CountIf(Range("A1:A" & i), FndWrd) = WrdCnt And _
            Not WorksheetFunction.CountIf(Range("A1:A" & i), FndWrd) = 1 And _
            Cells(i, "A").Value = FndWrd Then
            Rows(i).EntireRow.Delete
        End If
    Next i
    VBA Noob

  5. #5
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Fantastic, vbanoob.

    With a tiny bit of tweaking and combining the code works perfectly.

    I may, of course, post again looking for further help with this project if I get stuck again.

    Many thanks again

    Seamus

+ 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