Limited scope VLookup

Demirion

Board Regular
Joined
Sep 21, 2022
Messages
66
Platform
  1. Windows
Hi. I have a table in which I assign team members to specific tasks using the vlookup. I would like to each team member should have 20 requests assigned. The rest of requests should receive the status "unassigned". What's the easiest way to do this?
 
Corrected it. I even copied some code from the formula that worked earlier and nothing. Unfortunately, that's not the case :/
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I will try to explain it better. I used vlookup to assign employees to certain tasks but it doesn't matter. The point is, let's say in column A I have 300 different names. I would like to do so that if there are more than 20 of a specific name, e.g. Smith, in column A, then unassigned is displayed instead. So let's assume that column A to row 55 has 20 Smith surnames and from row 55 it should already show unassigned. Maybe a loop and a conditional statement will be the easiest way to do it?
 
Upvote 0
For example, I did it manually with a smaller number. If a name appears more than 3 times, unassigned is displayed. The end result should be like this:

example.PNG
 
Upvote 0
I think I nailed it :)

VBA Code:
Sub myFunction()
    Dim uniqueNames() As String
    Dim j As Integer
    j = 1
    Dim lRow As Integer
    'Get unique names to an array
    ReDim Preserve uniqueNames(1, 0)
    uniqueNames(0, 0) = Cells(1, 1).Value
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To lRow
        If Not Cells(i, 1).Value = uniqueNames(0, j - 1) Then
            ReDim Preserve uniqueNames(1, j)
            uniqueNames(0, j) = Cells(i, 1).Value
            j = j + 1
        End If
    Next
    j = j - 1
    'Set name counter to 0
    For i = 0 To j
      uniqueNames(1, i) = 0
    Next
    'Detect if appeared more than 20
    For i = 0 To j
        For ii = 1 To lRow
            If uniqueNames(0, i) = Cells(ii, 1).Value Then
                uniqueNames(1, i) = uniqueNames(1, i) + 1
                If uniqueNames(1, i) > 20 Then
                    Cells(ii, 1).Value = "unassigned"
                End If
            End If
        Next
    Next
End Sub
 
Upvote 0
Thanks, it works. Difficult code. It can be done in a simpler way without the use of an array?
 
Upvote 0
Maybe Something like this?

VBA Code:
Sub myFunction()
    Dim j As Integer
    Dim lRow As Integer
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To lRow
        If Not Cells(i, 1).Value = "unassigned" Then
            j = 0
            For ii = i + 1 To lRow
                If Cells(i, 1).Value = Cells(ii, 1).Value Then
                    j = j + 1
                    If j >= 20 Then
                        Cells(ii, 1).Value = "unassigned"
                    End If
                End If
            Next
        End If
    Next
End Sub
 
Upvote 0
Thanks, but there's one more problem. In the example file it works, but in another file I have the required variables and I don't know why but I get "type mismatch" with this code:
VBA Code:
If Cells(i, 1).Value = Cells(ii, 1).Value Then
I have tried different variables but nothing helps.
 
Upvote 0
Maybe you have dates or numbers.. I cannot say anything without seeing the actual data...
 
Upvote 0
Sorry for the confusion. I already know what's wrong. I have "# N / A" in the column. Could this be solved in some simple way? I think it's best to skip such cells.
 
Upvote 0
So a simple check will solve the problem:

VBA Code:
Sub myFunction()
    Dim j As Integer
    Dim lRow As Integer
    lRow = Cells(Rows.Count, 1).End(xlUp).Row


    For i = 1 To lRow
    If Not Application.IsNA(Cells(i, 1).Value) Then
            If Not Cells(i, 1).Value = "unassigned" Then
                j = 0
                For ii = i + 1 To lRow
                If Not Application.IsNA(Cells(ii, 1).Value) Then
                        If Cells(i, 1).Value = Cells(ii, 1).Value Then
                            j = j + 1
                            If j >= 20 Then
                                Cells(ii, 1).Value = "unassigned"
                            End If
                        End If
                    End If
                Next
            End If
        End If
    Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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