滚动查看有不同数量按钮的按钮
Scroll to view buttons where there are a varying number of buttons
我有 selected
个句子。
1) 句子可以变化。
2) 我有 split
个句子的每个单词。
下面的代码从 Selection
创建了 Word array
的列表。
Sub Seperate_Words()
Dim WrdArray() As String
WrdArray() = Split(Selection)
For i = LBound(WrdArray) To UBound(WrdArray)
strg = strg & vbNewLine & WrdArray(i)
Next i
MsgBox strg
End Sub
现在我想在每个单词前加一个Search button
在任何情况下,句子的长度都会改变,而用户表单是预先指定的,这就是我不能使用它们的原因。
下图显示了输出应该如何
现在我面临的问题是在框架中添加一个滚动条,它会根据需要动态更改。
我找到了一个非常有趣的解决方案:
创建一个用户窗体(我已将我的窗体命名为 "frmSearchForm")
在上面创建一个框架(我的是 "framTest")
创建一个类模块并命名为"clsUserFormEvents"
将此代码添加到其中:
Public WithEvents mButtonGroup As msforms.CommandButton
Private Sub mButtonGroup_Click()
'This is where you add your routine to do something when the button is pressed
MsgBox mButtonGroup.Caption & " has been pressed" 'Just Example Code
End Sub
然后在 ThisDocument 模块中,添加以下代码:
Dim mcolEvents As New Collection
Sub createButtonsOnForm()
Dim Cmd As msforms.CommandButton
'create instance of class
Dim cBtnEvents As clsUserFormEvents
'array for selection
Dim wordArr() As String
'get selection into array
wordArr = Split(Selection, " ")
Dim i As Integer
'counter for the top position of buttons
Dim topcounter As Integer
topcounter = 10
'loop through array
For i = LBound(wordArr) To UBound(wordArr) Step 1
'create button
Set Cmd = frmSearchForm.framTest.Controls.Add("Forms.CommandButton.1", "Test")
'Adjust properties of it
With Cmd
.Caption = wordArr(i)
.Left = 100
.Top = topcounter
.Width = 50
.Height = 20
End With
'Instantiate Class
Set cBtnEvents = New clsUserFormEvents
'Add cmd to event in class
Set cBtnEvents.mButtonGroup = Cmd
'Add buttonevent to collection so it won't get deleted in next iteration of the loop
mcolEvents.Add cBtnEvents
'increase the top position
topcounter = topcounter + 25
Next i
'show userform
frmSearchForm.Show
End Sub
然后如果你 运行 这个子,选择被分成数组,为每个元素创建一个按钮(选择部分作为标题),如果你按下按钮, class 被调用,您可以在其中使用 mButtonGroup.Caption
属性 来获取按钮的值。
示例:
我已经选择了单词 "Test1" 和 "Test2",现在当我 运行 Sub 时,表单打开时有 2 个按钮(测试 1 和测试 2):
我有 selected
个句子。
1) 句子可以变化。
2) 我有 split
个句子的每个单词。
下面的代码从 Selection
创建了 Word array
的列表。
Sub Seperate_Words()
Dim WrdArray() As String
WrdArray() = Split(Selection)
For i = LBound(WrdArray) To UBound(WrdArray)
strg = strg & vbNewLine & WrdArray(i)
Next i
MsgBox strg
End Sub
现在我想在每个单词前加一个Search button
在任何情况下,句子的长度都会改变,而用户表单是预先指定的,这就是我不能使用它们的原因。
下图显示了输出应该如何
现在我面临的问题是在框架中添加一个滚动条,它会根据需要动态更改。
我找到了一个非常有趣的解决方案:
创建一个用户窗体(我已将我的窗体命名为 "frmSearchForm")
在上面创建一个框架(我的是 "framTest")
创建一个类模块并命名为"clsUserFormEvents"
将此代码添加到其中:
Public WithEvents mButtonGroup As msforms.CommandButton
Private Sub mButtonGroup_Click()
'This is where you add your routine to do something when the button is pressed
MsgBox mButtonGroup.Caption & " has been pressed" 'Just Example Code
End Sub
然后在 ThisDocument 模块中,添加以下代码:
Dim mcolEvents As New Collection
Sub createButtonsOnForm()
Dim Cmd As msforms.CommandButton
'create instance of class
Dim cBtnEvents As clsUserFormEvents
'array for selection
Dim wordArr() As String
'get selection into array
wordArr = Split(Selection, " ")
Dim i As Integer
'counter for the top position of buttons
Dim topcounter As Integer
topcounter = 10
'loop through array
For i = LBound(wordArr) To UBound(wordArr) Step 1
'create button
Set Cmd = frmSearchForm.framTest.Controls.Add("Forms.CommandButton.1", "Test")
'Adjust properties of it
With Cmd
.Caption = wordArr(i)
.Left = 100
.Top = topcounter
.Width = 50
.Height = 20
End With
'Instantiate Class
Set cBtnEvents = New clsUserFormEvents
'Add cmd to event in class
Set cBtnEvents.mButtonGroup = Cmd
'Add buttonevent to collection so it won't get deleted in next iteration of the loop
mcolEvents.Add cBtnEvents
'increase the top position
topcounter = topcounter + 25
Next i
'show userform
frmSearchForm.Show
End Sub
然后如果你 运行 这个子,选择被分成数组,为每个元素创建一个按钮(选择部分作为标题),如果你按下按钮, class 被调用,您可以在其中使用 mButtonGroup.Caption
属性 来获取按钮的值。
示例:
我已经选择了单词 "Test1" 和 "Test2",现在当我 运行 Sub 时,表单打开时有 2 个按钮(测试 1 和测试 2):