Loop Through Range And Check Value In Column

mouse88

Board Regular
Joined
May 24, 2011
Messages
148
This may be easy to do but my minds on overtime today.

Heres the code I have so far:

Code:
'Look Up Name, Find And Return Total Of Selected Option'
Public Function GetShrinkage(Name As String, Activity As String, Report As Range) As Date
'-----------------'
'Declare Variables'
'-----------------'
Dim StartRowIndex As Long 'To Store The First Row Number'
Dim EndRowIndex As Long 'To Store The Last Row Number'
Dim ReturnValue As Date 'To Hold The Running Total'
Dim TestRange As Range 'To Hold The Resulting Range Of The Search'
'-------------'
'Set Variables'
'-------------'
ReturnValue = TimeValue("00:00:00") 'Set Starting Value So 00:00:00 Is Returned For No Match'
StartRowIndex = 0 'Set Initial Value'
EndRowIndex = 0 'Set Initial Value'
'--------------------'
'Get Start Row Number'
'--------------------'
Set TestRange = Report.Find(Name) 'Look For Name And Store Resulting Range'
'Test If Name Found'
If TestRange Is Nothing Then
    GetShrinkage = CVErr(xlErrNA)
    Exit Sub
End If
'Result Found/Store Start Row Index'
StartRowIndex = TestRange.Row
'--------------------'
'Get End Row Number'
'--------------------'
Dim LoopCounter As Long 'To Loop Through Rows'
LoopCounter = StartRowIndex 'Set Start Row Value'

    
End Function

Where I have the heading "Get Row End Number" I want to loop through column A of the range 'Report' that is passed as a parameter starting with the row number that is stored in 'StartRowIndex'. I want the loop to stop when the first part of the cells value = "Agent"
Code:
Left(Cell.Text, 5) = "Agent"

And then store the cells row number in EndRowIndex.

The only thing im not sure about is looping through column A in a range that has already been specified.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
With my changes in red, try:
Rich (BB code):
'Look Up Name, Find And Return Total Of Selected Option'
Public Function GetShrinkage(Name As String, Activity As String, Report As Range) As Date
'-----------------'
'Declare Variables'
'-----------------'
Dim StartRowIndex As Long 'To Store The First Row Number'
Dim EndRowIndex As Long 'To Store The Last Row Number'
Dim ReturnValue As Date 'To Hold The Running Total'
Dim TestRange As Range 'To Hold The Resulting Range Of The Search'
'-------------'
'Set Variables'
'-------------'
ReturnValue = TimeValue("00:00:00") 'Set Starting Value So 00:00:00 Is Returned For No Match'
StartRowIndex = 0 'Set Initial Value'
EndRowIndex = 0 'Set Initial Value'
'--------------------'
'Get Start Row Number'
'--------------------'
Set TestRange = Report.Find(Name) 'Look For Name And Store Resulting Range'
'Test If Name Found'
If TestRange Is Nothing Then
    GetShrinkage = CVErr(xlErrNA)
    Exit Sub
End If
'Result Found/Store Start Row Index'
StartRowIndex = TestRange.Row
'--------------------'
'Get End Row Number'
'--------------------'
Dim LoopCounter As Long 'To Loop Through Rows'
LoopCounter = StartRowIndex 'Set Start Row Value'

Dim cell As Range
For Each cell In TestRange
    If UCase(Left(cell.Value, 5)) = "AGENT" Then
        EndRowIndex = cell.Row
        Exit For
    End If
Next cell

End Function
An observation, personally, I found the above code over saturated with comments and explanations for what each part does, a bit OTT. Completely understand (and I do so myself) the need to comment code for other readers but there's a balance between too much that itself makes the code difficult to read/follow and too little that doesn't aid either. Just an opinion though.
 
Upvote 0
Couldn't you just use Find again to find the first row with a value beginning with your search term?
 
Upvote 0
How can I get it to only loop through column A of the range? The range thats passed to the function will always be ("A:Z").

I thought about using the find function but there will be over 250 rows that actually contain "Agent" and I need to specifically find the next one down after the StartRowIndex.

Thanks
 
Upvote 0
Change my bit in red to:
Code:
Dim i as Long
For i = 1 to Range("A" & Rows.Count).End(xlUp).Row
    If UCase(Left(Range("A" & i), 5)) = "AGENT" Then
        EndRowIndex = i
        Exit For
    End If
Next cell
 
Upvote 0
I managed to get it working with this:

Code:
Dim LoopCounter As Long
LoopCounter = StartRowIndex + 1
 
'Loop Rows Until Next Name Found'
Do Until Left(Report.Range("A" & LoopCounter).Text, 5) = "Agent"
    LoopCounter = LoopCounter + 1
Loop
 
EndRowIndex = LoopCounter 'Set End Row Index'

The problem I have now is when im doing the find on the name, if that particular name is the last one in the report it will never find another row containing "Agent" so it returns an error.

Any ideas how I can tell it is the last name on the report?

Report:
2v2b6km.png
 
Upvote 0
The For Loop I suggested wouldn't allow you to go past the last row that contained data
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top