Excel-VBA MultiPage:在运行时移动/重新排序/索引页面? [英] Excel-VBA MultiPage: move/reorder/index pages at runtime?
问题描述
我正在使用一些Excel-VBA GUI / Form,用户可以从.ini文件读取数据。 UserForm有一个MultiPage,其中用户在运行时创建页面,每个页面将代表一个ini部分。此外,这些部分也在主节中进行索引,以便进一步处理:此时,我循环遍历MultiPage页面以创建此索引。问题是,用户需要能够改变这个索引的顺序。现在,是否可以在运行时移动MultiPage中的页面?我正在考虑
I'm working on a little Excel-VBA GUI/Form for the user to read and write data from/to an .ini file. The UserForm has a MultiPage in which the user creates pages at runtime and each page will represent an ini section. Furthermore, these sections are also indexed in a master section for further processing: at this point I'm looping through the MultiPage pages to create this index. Problem is, the user needs to be able to change the order of this index. Now, is it possible to move the pages within a MultiPage at runtime? I was thinking something to the effect of
Me.MultiPage1.Pages(i).Index = i + 1
但显然这不行。或者,有没有办法可以传递一个:=或类似于Multipage.Pages.Add的任何工作呢?
如果没有这个工作,我想我将使用MoveUp / Down按钮创建一个单独的ListBox控件。打开任何更好的解决方案。
but obviously that doesn't work. Alternatively, is there a way I can pass a before:= or anything similar to Multipage.Pages.Add to work around it? If none of this works, I think I'll create a seperate ListBox control with MoveUp/Down buttons. Open for any better solutions.
推荐答案
说你有一个 UserForm
如下所示:
然后可以包括下面的示例代码移动 MultiPage
的页面
项目的顺序:
Then you can include the sample code below to move the order of the Page
items of the 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
这篇关于Excel-VBA MultiPage:在运行时移动/重新排序/索引页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!