gpt4 book ai didi

vba - Excel-VBA 多页 : move/reorder/index pages at runtime?

转载 作者:行者123 更新时间:2023-12-04 21:56:40 25 4
gpt4 key购买 nike

我正在开发一个小的 Excel-VBA GUI/Form,供用户从/向 .ini 文件读取和写入数据。 UserForm 有一个 MultiPage,用户在运行时在其中创建页面,每个页面将代表一个 ini 部分。此外,这些部分还在主部分中建立索引以供进一步处理:此时我正在循环通过 MultiPage 页面来创建此索引。问题是,用户需要能够更改此索引的顺序。现在,是否可以在运行时在 MultiPage 中移动页面?我在想一些大意的东西

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

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

最佳答案

假设你有一个 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

关于vba - Excel-VBA 多页 : move/reorder/index pages at runtime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43158667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com