I would like to generate the captions for the pages of an userform multipage by looping through some cell values, but unfortunately i get an error when the userform is initialized.
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
As follow up from comments, this one works:
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
Also note, that I've changed Dim pagecount As Integer
to Dim pagecount As Long
(because max value of integer is only 32768
). For your example it's unlikely to have more than 32768
pages, but in general it's a good practise to use Long
when determining last row.