Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-23-2025, 09:28 PM
gmaxey gmaxey is offline Return row index of all cells where text is found. Windows 10 Return row index of all cells where text is found. Office 2019
Expert
Return row index of all cells where text is found.
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default Return row index of all cells where text is found.

I have a very simply worksheet with the following content in column 1




Pears
Pears
Apples
Peaches
Pears


I need to return the row index of each cell containing the text "Pears"


I have cobbled together the code below which returns:
2, 5 and 1. Yes, that is the correct numbers but they are not in the correct order.


I have already discovered that if I add a column heading e.g.,


Fruit
Pears
Pears
Apples
Peaches
Pears


... then the code correctly returns 2, 3, and 6.


Some questions are included in the code.



Code:
Sub GetRowIndexes()
Dim lngIndex As Long
Dim oSheet As Worksheet
Dim oRng
Dim oCol As New Collection
  Set oSheet = ThisWorkbook.Worksheets(1)
    With oSheet.Columns(1)
      Set oRng = .Cells.Find(What:="Pears")
        'Why is the first instance not found if it is located in the first row? 

        If Not oRng Is Nothing Then
          On Error GoTo Err_GetOut
            Do Until oRng Is Nothing
              oCol.Add oRng.Row, CStr(oRng.Row)
              Set oRng = .FindNext(oRng) 

              'Is there a better way to escape the loop?
              'Is there a better way overall to achieve this objective?

            Loop
        End If
    End With
Err_GetOut:
  Set oRng = Nothing
  For lngIndex = 1 To oCol.Count
    Debug.Print oCol.Item(lngIndex)
  Next
lbl_Exit:
  Exit Sub
End Sub

Thank you!!
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VLOOKUP - Return cells that are not blank from range of specified cells djlw84 Excel 10 05-09-2022 11:59 AM
Index Match and return mutiple results from a table Bosslb Excel 7 09-25-2021 11:42 PM
need Index, Match, Max to return SUMPRODUCT? IRHSAM Excel 0 11-17-2017 09:48 AM
Index to return value based on 2 conditions george batshon Excel 2 01-28-2017 05:11 AM
Return row index of all cells where text is found. find a set of characters in a string and return a 0 (zero) if not found MaineLady Excel 2 11-05-2015 03:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:47 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft