如何使用 Word VBA 将多选列表框值返回到句子中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19551754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I return multi select listbox values into a sentence using Word VBA?
提问by JFrench
I've created a multi select listbox on a userform. There are 9 items in the listbox. How do I gather those selected items into a sentence?
我在用户表单上创建了一个多选列表框。列表框中有 9 个项目。我如何将这些选定的项目组合成一个句子?
The listbox contains reasons for returning a check. The items in the listbox are idenfiers or placeholders for a longer string, so the selection "unsigned", creates the returned string, "the check was not signed".
列表框包含退回支票的原因。列表框中的项目是较长字符串的标识符或占位符,因此选择“无符号”会创建返回的字符串“支票未签名”。
The user can select several reasons, so depending on the selections, I need sentence structure that formats as: "x, y, and z" OR "y and z" OR "z". (ex: "the check is not signed, the check is post-dated, and the check is a third-party check.")
用户可以选择多个原因,因此根据选择,我需要格式为“x、y 和 z”或“y 和 z”或“z”的句子结构。(例如:“支票没有签名,支票是过期的,支票是第三方支票。”)
It seems as though an array needs to be created from the selections, the selections counted, then an "If then" statement to create the sentence, but I'm stumped. I can count the selected items, I can create the sentence if only 1 item is chosen, but the compound sentence stumps me.
似乎需要从选择中创建一个数组,对选择进行计数,然后使用“If then”语句来创建句子,但我很难过。我可以计算所选项目,如果只选择一项,我可以创建句子,但复合句难倒我。
回答by David Zemens
I have this function which returns an array of selected items from a listbox. I have updated from my original answer to return a delimited string instead of an array of selected items:
我有这个函数,它从列表框中返回一组选定项目。我已经更新了我的原始答案以返回一个分隔的字符串而不是选定项目的数组:
Public Function GetSelectedItems(lBox As MSForms.ListBox) As String
'returns an array of selected items in a ListBox
Dim tmpArray() As Variant
Dim i As Integer
Dim selCount As Integer
selCount = -1
'## Iterate over each item in the ListBox control:
For i = 0 To lBox.ListCount - 1
'## Check to see if this item is selected:
If lBox.Selected(i) = True Then
'## If this item is selected, then add it to the array
selCount = selCount + 1
ReDim Preserve tmpArray(selCount)
tmpArray(selCount) = lBox.List(i)
End If
Next
If selCount = -1 Then
'## If no items were selected, return an empty string
GetSelectedItems = "" ' or "No items selected", etc.
Else:
'## Otherwise, return the array of items as a string,
' delimited by commas
GetSelectedItems = Join(tmpArray, ", ")
End If
End Function
You can call this by assigning to an array:
您可以通过分配给数组来调用它:
Dim mySentence as String
mySentence = GetSelectedItems(MyUserForm.MyListBox)
From that point, you could just replace the lastcomma with a " and" and you should be all set.
从那时起,您只需用“和”替换最后一个逗号,您就应该全部设置好了。
回答by Howard Renollet
This is very basic and I just quickly threw it together, but may work for what you're trying to do:
这是非常基本的,我只是很快地把它放在一起,但可能适用于你想要做的事情:
Private Sub ListBox1_Change()
If ListBox1.Selected(0) = True Then
msg1 = ListBox1.List(0)
End If
If ListBox1.Selected(1) = True Then
msg2 = ListBox1.List(1)
End If
If ListBox1.Selected(2) = True Then
msg3 = ListBox1.List(2)
End If
MsgBox msg1 & ", " & msg2 & ", " & msg3
End Sub
回答by Eve
Forget the REDim array concept- can get confusing. A simple way to collect multiselect choices is the following
忘记 REDim 数组概念 - 可能会令人困惑。收集多选选项的简单方法如下
Sub SelectMulti()
picklist1 = ""
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Debug.Print i ' optional
If picklist1 = "" Then
picklist1 = ListBox1.List(i)
Else
picklist2 = ListBox1.List(i)
picklist1 = picklist1 & ";" & picklist2
End If
End If
Next
Debug.Print picklist1
End sub