Loop: Do Until Fill Color is 16 Debug Error

ryger48

New Member
Joined
Oct 20, 2012
Messages
3
Hello everyone,

I am familiar with a few coding languages, including PHP, C++, etc, however I am new to Excel and VBA. I am attempting to create a do-while/do-until loop which stays in the same column, and keeps descending row to row until it hits a Gray-filled cell, at which point it takes the value of that cell and returns it. The data looks something like this:

Sam47344358
544434585

334223456885
Mark23443236534523434
234234654534565
745456334543

<tbody>
</tbody>

(It was easier in this post to change the font color instead of the fill color, but you get the idea). Also, the number of rows between names varies. What I have written so far is this:

Code:
Function checkFill()


Dim i As Integer
Dim AHT As Integer


i = 0


Do While Cells(i, 5).Interior.ColorIndex > 0
    If Cells(i, 5).Interior.ColorIndex = 16 Then
        AHT = Cells(i, 5).Value
        Exit Do
    End If
    i = i + 1
Loop


End Function

Should I use a function here, or sub? Also, I keep getting an "Application-defined or object-defined error" message. What am I doing wrong/is there an easier way?

Thanks in advance for any help with this, I greatly appreciate it!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the MrExcel board!

1. You are getting the error with the following line ..

Do While Cells(i, 5).Interior.ColorIndex > 0

.. because the first time the code gets to that line i=0. That is, you are trying to check the colour of a cell in row 0, which doesn't exist.
So you could set i=1 to start to solve that.

2. If your non-gray cells have no fill then your code will finish prematurely because the interior.colorindex will return -4142 for a non-filled cell.
There could be various ways to fix that, also depending on the next piont.

3. Is it special that the cells are gray or are you really just looking for the last number in column E before the second name (Mark in your example)?
I ask because there may be a simpler way than checking each cell's colour.
 
Upvote 0
Thank you for your response!

1. Ah, that makes sense. I must have been thinking the 0 would be the first in line, like in an array, etc.

2. I changed the code to "Do Until Cells(i, 5).Interior.ColorIndex = 16" to get around that, now the code is compiling.

3. Yes, that's correct. In the reports I am getting, the gray sections are always the last line for each agent and contain a particular number, in this case an average handle time, for that agent. But because the number of rows differs between agents, based on how many calls they've taken, I wasn't sure how else to zero in on their average handle time. If there is a simpler way, I would love to employ it.
 
Upvote 0
3. Yes, that's correct. In the reports I am getting, the gray sections are always the last line for each agent and contain a particular number, in this case an average handle time, for that agent. But because the number of rows differs between agents, based on how many calls they've taken, I wasn't sure how else to zero in on their average handle time. If there is a simpler way, I would love to employ it.
Whether you use a Function or a Sub depends a bit on just how/where you want to use this and also personal choice.

I've used a function since that is how you started out. Using a Function, also allows it to be used in the worksheet as I've shown with a few examples in column K below.
Code:
Function FindVal(Agent As String, Optional Col As String = "E", Optional NameCol As String = "A")
  Dim myRow As Long, LastRow As Long
  Dim AgentStart As Range
  
  Set AgentStart = Columns(NameCol).Find(What:=Agent, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
  If AgentStart Is Nothing Then
    FindVal = "Agent not found"
  Else
    LastRow = Cells(Rows.Count, Col).End(xlUp).Row
    myRow = Columns(NameCol).Find(What:="*", After:=AgentStart, _
      LookIn:=xlValues, SearchDirection:=xlNext, SearchFormat:=False).Row - 1
    If myRow = 0 Then myRow = LastRow
    FindVal = Cells(myRow, Col).Value
  End If
End Function

The Function can then be used in code like this ..
Code:
Sub Test()
  Dim x As Variant
  
  x = FindVal("Sam")
  
'  Other examples:
'  x = FindVal("Mark", "F")
'  x = FindVal("Sam", "B", "A")
'  x = FindVal("Tom")

  MsgBox x
  
End Sub
.. or in the worksheet like this ..

Excel Workbook
ABCDEFGHIJKL
1NameVal 1Val 2Val 3Val 4Val 5Val 6Val 734
2Sam47344358345
3544434585Agent not found
4334223456885
5Mark23443236534523434
6234234654534565
7745456334543
8
Find value
 
Upvote 0
I have had the opportunity to test what you wrote, and it works beautifully! Thank you SO much!
 
Upvote 0
I have had the opportunity to test what you wrote, and it works beautifully! Thank you SO much!
Cheers, glad it worked for you.
A lot simpler than cycling through looking for a cell colour! :cool:
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,851
Members
449,411
Latest member
adunn_23

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