Domanda

Vorrei generare le didascalie per le pagine di un multipagine di un utente eseguito looping attraverso alcuni valori di cella, ma sfortunatamente ottengo un errore quando l'utente è inizializzato.

Private Sub UserForm_Initialize()

Dim pagecount As Integer
Dim n As Integer
Dim name As Integer

pagecount = Sheets("SETUP").Range("B" & rows.Count).End(xlUp).Row - 5
n = 0
name = 6

With MultiPage1
Do
.Pages(n).Caption = Sheets("SETUP").Range("B" & name).Value
n = n + 1
name = name + 1
Loop Until n = pagecount
End With

End Sub
.

È stato utile?

Soluzione

Come follow-up dai commenti, questo funziona:

Private Sub UserForm_Initialize()
    Dim pagecount As Long
    Dim n As Long

    pagecount = Sheets("SETUP").Range("B" & Rows.Count).End(xlUp).Row - 5
    n = 0

    With MultiPage1
        Do
            If n >= .Pages.Count Then .Pages.Add
            .Pages(n).Caption = Sheets("SETUP").Range("B" & n + 6).Value
            n = n + 1
        Loop Until n = pagecount
    End With
End Sub
.

Nota anche, che ho cambiato Dim pagecount As Integer su Dim pagecount As Long (poiché il valore massimo di Integer è solo 32768).Per il tuo esempio è improbabile che abbia più delle pagine 32768, ma in generale è una buona pratica usare Long durante la determinazione dell'ultima riga.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top