samedi 22 août 2020

VBA Randomize values from 1 to 30 thereby skipping manually entered values

For the first 15 ranks, I want to manually enter the values in B2:P11. For ranks 16 to 30, I want to randomize these values using an Excel VBA button, with the following code:

Sub rand_group()
    Dim i As Long
    Dim j As Long
    Dim myFlag(1 To num_man)
    Dim s_group As Worksheet
    Set s_group = Worksheets("group")


    'óêêîånóÒÇèâä˙âª
    Randomize
    
    s_group.Cells.Clear
    s_group.Range("A1") = "group_id"
    
    For i = 1 To num_group
      s_group.Cells(i + 1, 1) = i
    Next i
    For i = 1 To num_man
      s_group.Cells(1, i + 1) = "m_rank" & i
    Next i
    
    For i = 1 To num_group
      For j = 16 To num_man
        myFlag(j) = False
      Next j
      For j = 16 To num_man
        Do
          'óêêî=Int((ç≈ëÂíl - ç≈è¨íl +1 ) * Rnd + ç≈è¨íl)
          myNum = Int((num_man - 1 + 1) * Rnd + 1)
        Loop Until myFlag(myNum) = False
        
        s_group.Cells(i + 1, j + 1).Value = myNum
        myFlag(myNum) = True
      Next j
    Next i

End Sub

However, these random values should neglect the manually entered values in B2:P11

How can I change the code to fix this?

Screenshot of the excel file is displayed below: I want to manually fill values from B2 to P11

Thank you in advance for your response!

Michiel




Aucun commentaire:

Enregistrer un commentaire