Aide sur une macro : erreur d'exécution '9

Bonjour les amis!

Je vous joins un code en spoiler ci-dessous.

Lorsque je l'exécute, j'ai le message suivant : Erreur d'exécution '9' : L'indice n'appartient pas à la sélection.

Et il me surligne la ligne suivante :

tSelectIndex(iNextIndex) = iCardIndex

Je n'arrive pas à régler ce problème.

Une âme charitable pour m'aider?

Si besoin, je pourrais fournir le fichier excel dans lequel s'applique cette macro.

Sub DrawCard()

    iNewCard = InputBox("Soyez sûr qu'il en reste", "Nombre de cartes à piocher")
    iHandId = InputBox("Soyez sûr qu'il existe", "Pour quel numéro de setup ?")

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False

    Dim iDeckSize As Byte
    iDeckSize = Worksheets("Deck").Range("AS8")

    Dim tDeckCard() As String
    ReDim tDeckCard(iDeckSize - 1)
    Dim tDeckCost() As Variant
    ReDim tDeckCost(iDeckSize - 1)
    Dim iDeckIndex As Byte
    iDeckIndex = 0

    Dim tSheetName(3) As String
    tSheetName(0) = "ExtractA"
    tSheetName(1) = "ExtractC"
    tSheetName(2) = "ExtractE"
    tSheetName(3) = "ExtractL"

    Dim iCol As Byte
    Dim iLine As Byte
    Dim sCardName As String
    Dim iCardCost As Variant
    Dim iCardQuantity As Byte
    Dim iCardStored As Byte

    For Each ssheet In tSheetName
        iCol = 1
        iLine = 2
        sCardName = Worksheets(ssheet).Cells(iLine, iCol) 'WARNING: Cells(line, col) unlike in excel himself
        While sCardName <> "":
            iCardQuantity = Worksheets(ssheet).Cells(iLine, iCol + 1)
            iCardCost = Worksheets(ssheet).Cells(iLine, iCol + 3)
            iCardStored = 0
            While iCardStored < iCardQuantity
                tDeckCard(iDeckIndex) = sCardName
                tDeckCost(iDeckIndex) = iCardCost
                iDeckIndex = iDeckIndex + 1
                iCardStored = iCardStored + 1
            Wend
            iLine = iLine + 1
            sCardName = Worksheets(ssheet).Cells(iLine, iCol)
        Wend
    Next ssheet

    Dim iSeed As Byte
    Worksheets("DB").Activate
    Cells(2, 1).Activate
    Randomize iSeed

    'Retrieve already present card
    Dim tSelectedIndex() As Integer
    Dim iNextIndex As Byte
    ReDim tSelectedIndex(iDeckSize - 1)
    Dim iFillIndex As Byte
    For iFillIndex = 0 To iDeckSize - 1
        tSelectedIndex(iFillIndex) = -1
    Next iFillIndex
    iNextIndex = 0
    iCardIndex = Worksheets("Setup").Cells(6 + NextIndex, 3 + 4 * iHandId)
    While iCardIndex <> "":
        tSelectedIndex(iNextIndex) = iCardIndex
        iNextIndex = iNextIndex + 1
        iCardIndex = Worksheets("Setup").Cells(6 + NextIndex, 3 + 4 * iHandId)
    Wend

    Dim iSetupOk As Byte
    Dim bKeep As Boolean

    Dim iCurrentTry As Byte
    iCurrentTry = iHandId

    Dim iAddCard As Byte
    iAddCard = 0
    Dim iTableIndex As Byte
    iTableIndex = iNextIndex

    'Select new cards
    While iAddCard < iNewCard
        'Select a random value
        iRandIndex = Int((iDeckSize * Rnd))
        bKeep = True
        'Check if randomValue is not already selected
        For Each iValue In tSelectedIndex
            If iRandIndex = iValue Then
                bKeep = False
                Exit For
            End If
            If iValue = -1 Then
                Exit For
            End If
        Next iValue
        'If not already present, store it
        If bKeep = True Then
            tSelectedIndex(iTableIndex) = iRandIndex
            iTableIndex = iTableIndex + 1
        End If
    Wend

    'Print added Card
    iDeckIndex = 0
    For iCount = iNextIndex To iTableIndex
        iDeckIndex = tSelectedIndex(iCount)
        Sheets("Setup").Cells(6 + iCount, 1 + 4 * iHandId) = tDeckCard(iDeckIndex) 'First Cell A6 = Cells(6,1) '=> Je suis passé à l'horizontalité
        Sheets("Setup").Cells(6 + iCount, 2 + 4 * iHandId) = tDeckCost(iDeckIndex) 'First Cell B6 = Cells(6,2) '=> Idem
        Sheets("Setup").Cells(6 + iCount, 3 + 4 * iHandId) = tSelectedIndex(iCount)
    Next iCount

    Worksheets("Setup").Activate

    'Features reactivate
    Application.EnableEvents = True
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub

Bonjour,

probablement que tu as oublié de mettre un i devant certains de tes NextIndex

dans ce bout de code-ci

    iNextIndex = 0
    iCardIndex = Worksheets("Setup").Cells(6 + NextIndex, 3 + 4 * iHandId)
    While iCardIndex <> "":
        tSelectedIndex(iNextIndex) = iCardIndex
        iNextIndex = iNextIndex + 1
        iCardIndex = Worksheets("Setup").Cells(6 + NextIndex, 3 + 4 * iHandId)
    Wend

qui devrait être celui-ci. Je n'ai pas cherché à comprendre la logique, j'ai juste regardé ton code au niveau de la structure, de la syntaxe et des variables utilisées.

    iNextIndex = 0
    iCardIndex = Worksheets("Setup").Cells(6 + iNextIndex, 3 + 4 * iHandId)
    While iCardIndex <> "":
        tSelectedIndex(iNextIndex) = iCardIndex
        iNextIndex = iNextIndex + 1
        iCardIndex = Worksheets("Setup").Cells(6 + iNextIndex, 3 + 4 * iHandId)
    Wend

Oh! La quiche!

Merci pour ce regard neuf.

Tellement persuadé de ce que j'avais écris que je n'ai pas vu l'erreur.

Rechercher des sujets similaires à "aide macro erreur execution"