Can anyone help completing this working code?

chriz74

New Member
Joined
Jul 12, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I asked chatgpt to produce code and after trial and error it gave something that actually works. however it produced one that is not discriminating by judge types as I requested.
I told it judges from 1 to 8 are ordinary while 9 to 12 are special, and to assign a new judge in the third column based on the value of second column (ordinary or special) and turns, also the judge can't be the one in the first column.
It did something but then it takes the name of the judge from the array it made calling it judges while those are the turns of the judges to be assigned.
I need to modify the code so that there is actual dicrimination between ordinary and special in assigning a name, respecting the turn. Here is the code:

VBA Code:
Sub AssignJudges()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim judgeCounter As Long
    Dim turnCounter As Long
    Dim judges As Variant
    
    Set ws = ThisWorkbook.Worksheets("Rotation") 
    
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Assuming the data starts from column A
    
    ' Define the judges and their rotation turns
    judges = Array("Judge1", "Judge2", "Judge2", "Judge2", "Judge3", "Judge3", "Judge4", "Judge4", "Judge5", "Judge5", "Judge5", "Judge6", "Judge6", "Judge7", "Judge7", "Judge8", "Judge8", "Judge8", "Judge9", "Judge9", "Judge10", "Judge11", "Judge11", "Judge12", "Judge12")
    
    ' Start assigning judges from the first row to the last row
    For i = 2 To lastRow
        If ws.Cells(i, 1).value <> "" And ws.Cells(i, 2).value <> "" Then
            If ws.Cells(i, 2).value = "Common" Then
                ' Assign any judge since the Kind is "Common"
                ws.Cells(i, 3).value = GetNextJudge(judgeCounter, judges, turnCounter)
            ElseIf ws.Cells(i, 2).value = "Ordinary" Then
                ' Assign only ordinary judges since the Kind is "Ordinary"
                ws.Cells(i, 3).value = GetNextOrdinaryJudge(judgeCounter, judges, turnCounter)
            End If
        End If
    Next i
End Sub

Function GetNextJudge(ByRef counter As Long, ByVal judges As Variant, ByRef turnCounter As Long) As String
    ' Increment the judge counter
    counter = counter + 1
    
    ' Wrap around the counter if it exceeds the number of judges
    If counter > UBound(judges) Then
        counter = 0
    End If
    
    ' Increment the turn counter if it's the judge's first turn
    If counter = 0 Then
        turnCounter = turnCounter + 1
    End If
    
    ' Return the next judge
    GetNextJudge = judges(counter)
End Function

Function GetNextOrdinaryJudge(ByRef counter As Long, ByVal judges As Variant, ByRef turnCounter As Long) As String
    ' Increment the ordinary judge counter
    counter = counter + 1
    
    ' Wrap around the counter if it exceeds the number of ordinary judges
    If counter > UBound(judges) Then
        counter = 0
    End If
    
    ' Increment the turn counter if it's the judge's first turn
    If counter = 0 Then
        turnCounter = turnCounter + 1
    End If
    
    ' Return the next ordinary judge
    GetNextOrdinaryJudge = judges(counter)
End Function
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
First glance it looks like "counter" and "turncounter" are not assigned a value outside of the functions.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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