2017-04-01 166 views
1

我正在使用一个小的Excel-VBA GUI /窗体供用户读取/写入.ini文件中的数据。 UserForm有一个MultiPage,用户在运行时创建页面,每个页面将代表一个ini部分。此外,这些部分还在主节中编入索引以供进一步处理:此时,我循环浏览MultiPage页面以创建此索引。问题是,用户需要能够改变这个索引的顺序。现在,是否有可能在运行时移动多页中的页面?我在想什么的效果Excel-VBA MultiPage:在运行时移动/重新排序/索引页面?

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

但显然这是行不通的。或者,有没有一种方法可以传递一个before:=或者类似于Multipage.Pages.Add的东西来解决它? 如果这些都不起作用,我想我会用MoveUp/Down按钮创建一个单独的ListBox控件。打开任何更好的解决方案。

回答

2

假设你有一个UserForm,看起来像这样:

enter image description here

然后你可以包括下面的示例代码移动Page项目MultiPage的顺序:

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 
+0

非常感谢罗宾,完美。所以Page.Index确实存在,我只需要将页面声明为一个单独的对象,对吗?由于我的页面是在运行时创建的,所以我不得不修改代码并放入一个类中。我将在解决问题的过程中发布解决方案,希望这是适当的过程。谢谢。 –

+0

嗨,很高兴它有帮助。如果您有解决问题的代码更新,您可以发布自己问题的答案。这可能是一个更好的选择,而不是改变问题。 –

+0

@RobinMackenzie不错:) –

1

对于任何人在未来寻找这个,这里是使用Robin代码的完整解决方案(谢谢!),但是在运行时创建的页面放入一个类。我只是将相关的代码粘贴到这个问题上,用户也可以调用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