mardi 10 octobre 2017

Randomize List VBA

I'm fairly new to VBA.

I'm trying to randomize a list with VBA. The list has two headers "Name" and "Dials". I want to try to randomize the list with a macro then applying it with a button. I've tried using the code below but and it randomizes the names and numbers but doesn't keep them together. Meaning If My name is Jon and I had 3 dials, it moves my dials some where else. Any help would be appreciated.

Thanks,

Sub Random()
        Dim tempString As String
        Dim tempInteger As Integer
        Dim i As Integer
        Dim j As Integer

        For i = 1 To 5
          Cells(i, 2).Value = WorksheetFunction.RandBetween(0, 1000)
        Next i

    For i = 1 To 5
        For j = i + 1 To 5

      If Cells(j, 2).Value < Cells(i, 2).Value Then

        tempString = Cells(i, 2).Value
        Cells(i, 2).Value = Cells(j, 2).Value
        Cells(j, 2).Value = tempString
        tempInteger = Cells(i, 2).Value
        Cells(i, 2).Value = Cells(j, 2).Value
        Cells(j, 2).Value = tempInteger

      End If

        Next j
    Next i
End Sub




Aucun commentaire:

Enregistrer un commentaire