通过循环单元格值添加多页标题

时间:2014-04-14 15:14:53

标签: vba excel-vba userform multipage do-loops

我想通过循环遍历某些单元格值来生成userform多页面的标题,但不幸的是,初始化用户窗体时出现错误。

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

1 个答案:

答案 0 :(得分:0)

作为评论的后续内容,这个有效:

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

另请注意,我已将Dim pagecount As Integer更改为Dim pagecount As Long(因为整数的最大值仅为32768)。对于您的示例,它不可能超过32768页面,但一般来说,在确定最后一行时使用Long是一种很好的做法。