Excel-VBA MultiPage:在运行时移动/重新sorting/索引页面?

我正在使用一个小的Excel-VBAgraphics用户界面/窗体来读取/写入.ini文件的数据。 用户窗体有一个多页,用户在运行时创build页面,每个页面将代表一个ini部分。 此外,这些部分也在主要部分进行索引以便进一步处理:此时,我循环访问MultiPage页面以创build此索引。 问题是,用户需要能够改变这个索引的顺序。 现在,是否有可能在运行时移动多页中的页面? 我正在想一些事情的效果

Me.MultiPage1.Pages(i).Index = i + 1 

但显然这是行不通的。 或者,有没有办法我可以通过一个之前:=或任何类似于Multipage.Pages.Add解决它? 如果这些都不起作用,我想我会用MoveUp / Downbutton创build一个单独的ListBox控件。 打开任何更好的解决scheme。

假设你有一个UserForm ,如下所示:

在这里输入图像说明

然后,您可以在下面包含示例代码来移动MultiPagePage项目的顺序:

 Option Explicit 'moves selected page to left Private Sub CommandButton1_Click() Dim pag As MSForms.Page Dim lngPageCount As Long ' get reference to page Set pag = Me.MultiPage1.SelectedItem ' get number of pages in multipage lngPageCount = Me.MultiPage1.Pages.Count ' check if trying to go left beyond first page and put to end ' otherwise decrement pages position in multipage If pag.Index = 0 Then pag.Index = lngPageCount - 1 Else pag.Index = pag.Index - 1 End If ' update caption Me.Label1.Caption = pag.Name & " is at index " & pag.Index End Sub 'moves selected page to right Private Sub CommandButton2_Click() Dim pag As MSForms.Page Dim lngPageCount As Long ' get reference to page Set pag = Me.MultiPage1.SelectedItem ' get number of pages in multipage lngPageCount = Me.MultiPage1.Pages.Count ' check if trying to go right beyond number of pages and put to start ' otherwise increment pages position in multipage If pag.Index = lngPageCount - 1 Then pag.Index = 0 Else pag.Index = pag.Index + 1 End If ' update caption Me.Label1.Caption = pag.Name & " is at index " & pag.Index End Sub 

对于任何人在未来寻找这个,这是完整的解决scheme使用罗宾的代码(谢谢!),但在运行时创build的页面放入一个类。 我只是粘贴相关的代码到这个问题,CopyPage过程也可以被用户调用,以在运行时添加页面。 现在用户也可以在页面2(索引1)和n之间左右移动它们。

在我的主要模块中:

 Public arrLeftButton() As New CButton Public arrRightButton() As New CButton 

在我的CButton类模块中:

 Option Explicit Public WithEvents CopyButton As MSForms.CommandButton Public WithEvents DeleteButton As MSForms.CommandButton Public WithEvents MoveLeft As MSForms.CommandButton Public WithEvents MoveRight As MSForms.CommandButton Private Sub MoveLeft_Click() Dim pag As MSForms.Page Dim lngPageCount As Long Set pag = UFmodproject.MultiPage1.SelectedItem lngPageCount = UFmodproject.MultiPage1.Pages.Count If pag.Index > 1 Then pag.Index = pag.Index - 1 End If End Sub Private Sub MoveRight_Click() Dim pag As MSForms.Page Dim lngPageCount As Long Set pag = UFmodproject.MultiPage1.SelectedItem lngPageCount = UFmodproject.MultiPage1.Pages.Count If pag.Index < lngPageCount - 1 Then pag.Index = pag.Index + 1 End If End Sub 

和我的UserForm_Initialize:

 Private Sub userform_initialize() ReDim Preserve arrLeftButton(1 To 1) ReDim Preserve arrRightButton(1 To 1) Set arrLeftButton(1).MoveLeft = MultiPage1.Pages(1).Controls("MoveLeft1") Set arrRightButton(1).MoveRight = MultiPage1.Pages(1).Controls("MoveRight1") For i = 2 To GetINIString("Project", "NumberOfShipmentTypes", strINIPATH) Call FormControls.CopyPage Next End Sub 

而在另一个标准模块中,也可以从其他地方调用:

 Sub CopyPage() Dim l As Double, r As Double Dim Ctrl As Control Dim newCtrl As Object Dim pCount As Long pCount = UFmodproject.MultiPage1.Pages.Count '[...add pages and copy all controls] For Each newCtrl In UFmodproject.MultiPage1.Pages(pCount).Controls If Left(newCtrl.Name, Len(newCtrl.Name) - 1) = "MoveLeft" Then ReDim Preserve arrLeftButton(1 To pCount) Set arrLeftButton(pCount).MoveLeft = newCtrl End If If Left(newCtrl.Name, Len(newCtrl.Name) - 1) = "MoveRight" Then ReDim Preserve arrRightButton(1 To pCount) Set arrRightButton(pCount).MoveRight = newCtrl End If Next End Sub