Hello,

I have this userform containing a command button. Upon hitting the command button, the code should create a unique (no diplicates) random number. This number is linked to a picture file which is stored in a certain file. This picture opens in a userform. Certain cells are shown in this picture and these can be scored (selection of option in combobox). The correct scores are given in a second excel file (Categorization.xlsx). These scores are checked against the answers chosen in the comboboxes. For the latter, I managed to write a code (see below). However, I don't know how to create unique random numbers. Also, there should be a message when all pictures are scored.
Please find the code below.
Thank you in advance,
Veerle

Option Explicit
Dim number As Integer
Dim folder As String
Dim myPath As String
Dim extension As String
Dim filename As String
Dim arg As String
Dim p As String
Dim f As String
Dim s As String
Dim a As String
Dim b As String
Dim c As String
Dim cellnumber1 As String
Dim cellnumber2 As String
Dim cellnumber3 As String
Dim Chosencellnumber1 As String
Dim Chosencellnumber2 As String
Dim Chosencellnumber3 As String
Dim lowerbound As Integer
Dim upperbound As Integer

Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
    Dim arg As String
'   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
'   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)
'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
End Function


Public Sub CommandButton1_Click()

'Reset comboboxes with every new picture
ComboBox1.Value = Null
ComboBox2.Value = Null
ComboBox3.Value = Null

'Reset color and text of labels with every new picture

Label6.BackColor = &H80000002
Label7.BackColor = &H80000002
Label8.BackColor = &H80000002
Label6.Caption = ""
Label7.Caption = ""
Label8.Caption = ""

'Add text of comboboxes

ComboBox1.AddItem "Non-fragmented cell"
ComboBox1.AddItem "Darkly stained cell"
ComboBox1.AddItem "Cell with large halo"
ComboBox1.AddItem "Microcephalic head"
ComboBox1.AddItem "Degraded cell"
ComboBox1.AddItem "Violet cell"
ComboBox1.AddItem "Not included"

ComboBox2.AddItem "Non-fragmented cell"
ComboBox2.AddItem "Darkly stained cell"
ComboBox2.AddItem "Cell with large halo"
ComboBox2.AddItem "Microcephalic head"
ComboBox2.AddItem "Degraded cell"
ComboBox2.AddItem "Violet cell"
ComboBox2.AddItem "Not included"

ComboBox3.AddItem "Non-fragmented cell"
ComboBox3.AddItem "Darkly stained cell"
ComboBox3.AddItem "Cell with large halo"
ComboBox3.AddItem "Microcephalic head"
ComboBox3.AddItem "Degraded cell"
ComboBox3.AddItem "Violet cell"
ComboBox3.AddItem "Not included"

' choosing random picture number
lowerbound = 1
upperbound = 10
Randomize
number = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Label5.Caption = number
Debug.Print number
        
' compose dir of picture
myPath = ActiveWorkbook.path
folder = myPath & "\Numbers"
extension = ".jpg"
filename = folder & "\number" & number & extension

' Find categorization of cells associated with the numbers on the picture
p = "C:\Temp\DNA fragmentatie\tweede versie"
f = "Categorization.xlsx"
s = "Blad1"
a = "B" & number
b = "C" & number
c = "D" & number
cellnumber1 = GetValue(p, f, s, a)
cellnumber2 = GetValue(p, f, s, b)
cellnumber3 = GetValue(p, f, s, c)

' Hide comboboxes when no cell number is given
If cellnumber1 <> "0" Then
UserForm1.ComboBox1.Visible = True
UserForm1.Label2.Visible = True
UserForm1.Label6.Visible = True
Else
UserForm1.ComboBox1.Visible = False
UserForm1.Label2.Visible = False
UserForm1.Label6.Visible = False
End If

If cellnumber2 <> "0" Then
UserForm1.ComboBox2.Visible = True
UserForm1.Label3.Visible = True
UserForm1.Label7.Visible = True
Else
UserForm1.ComboBox2.Visible = False
UserForm1.Label3.Visible = False
UserForm1.Label7.Visible = False
End If

If cellnumber3 <> "0" Then
UserForm1.ComboBox3.Visible = True
UserForm1.Label4.Visible = True
UserForm1.Label8.Visible = True
Else
UserForm1.ComboBox3.Visible = False
UserForm1.Label4.Visible = False
UserForm1.Label8.Visible = False
End If

      
' load picture
Image1.Picture = LoadPicture(filename)

End Sub
Private Sub CommandButton2_Click()

Chosencellnumber1 = ComboBox1.Value
Chosencellnumber2 = ComboBox2.Value
Chosencellnumber3 = ComboBox3.Value

' Check whether option chosen in box 1 is correct
If Chosencellnumber1 <> "" Then
    If Chosencellnumber1 = cellnumber1 Then
    Label6.BackColor = &HC000&
    Else
    Label6.BackColor = &HFF&
    End If
Else
Label6.BackColor = &H80000005
End If

' Check whether option chosen in box 2 is correct
If Chosencellnumber2 <> "" Then
    If Chosencellnumber2 = cellnumber2 Then
    Label7.BackColor = &HC000&
    Else
    Label7.BackColor = &HFF&
    End If
Else
Label7.BackColor = &H80000005
End If

' Check whether option chosen in box 3 is correct
If Chosencellnumber3 <> "" Then
    If Chosencellnumber3 = cellnumber3 Then
    Label8.BackColor = &HC000&
    Else
    Label8.BackColor = &HFF&
    End If
Else
Label8.BackColor = &H80000005
End If

Label6.Caption = cellnumber1
Label7.Caption = cellnumber2
Label8.Caption = cellnumber3
End Sub