Word 2013- VBA- 如何向用户表单列表框中的结果添加标点符号?
Word 2013- VBA- How To Add Punctuation to Results From A User Form List Box?
我有一个包含四个条目的列表框(多选)的用户表单。
以下代码构建字符串:
Dim myArray() As String
'Use Split function to return a zero based one dimensional array.
myArray = Split("text 1|" _
& "text 2|" _
& "text 3|" _
& "text 4", "|")
'Use .List method to populate listbox.
ListBox1.List = myArray
'Use the .ColumnWidth property to set column widths. 0 results in a hidden column.
ListBox1.ColumnWidths = "1"
lbl_Exit:
Exit Sub
下面的代码成功插入了四个选定条目的任意组合,每个条目之间带有 ,
和 space,包括末尾的 .
,即 text 1, .
或 text 1, text 2, text 3, .
或 text 1, text 2, text 3, text 4, .
到内容控件中。
Dim SelectedTexts As String
Dim index As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
'Adds comma after every entry
SelectedTexts = SelectedTexts & ListBox1.List(index) & ", "
End If
Next
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
Unload Me
lbl_Exit:
Exit Sub
Me.Repaint
Me.Hide
我正在努力完成两件事:
- 我只想在最后一个条目后添加一个
.
,即 text 1.
而不是 text 1, .
- 我希望在最后一个结果之前有一个
and
,即 text 1 and text 2.
或 text 1, text 2 and text 3.
或 text 1, text 2 and text 4.
构建字符串后,您需要将其操作成您需要的形式。这是执行此操作的逻辑:
Private Sub Test()
Dim SelectedTexts As String
Dim index As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
SelectedTexts = SelectedTexts & ListBox1.List(index) & ", "
End If
Next
SelectedTexts = Mid(SelectedTexts, 1, Len(SelectedTexts) - 2) & "."
index = InStrRev(SelectedTexts, ",")
If index > 0 Then SelectedTexts = Left(SelectedTexts, index - 1) & " and " & Right(SelectedTexts, Len(SelectedTexts) - index - 1)
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = SelectedTexts
End Sub
编辑:调整大小写顺序
编辑 2:重构代码
Dim SelectedTexts As String
Dim index As Long
' Get total selected
Dim totalSelected As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
totalSelected = totalSelected + 1
End If
Next
' Convert total selected to base 0
totalSelected = totalSelected - 1
Dim counter As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
'Adds comma after every entry
Dim delimiter As String
Select Case True
Case counter = totalSelected - 1
delimiter = " and "
Case counter < totalSelected
delimiter = ", "
Case Else
delimiter = ""
End Select
SelectedTexts = SelectedTexts & ListBox1.List(index) & delimiter
counter = counter + 1
End If
Next
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
Unload Me
lbl_Exit:
Exit Sub
Me.Repaint
Me.Hide
只需更改:
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
至:
'Adds "and", plus period to the end
SelectedTexts = Left(SelectedTexts, Len(SelectedTexts) - 2) & "."
SelectedTexts = Left(SelectedTexts, InStrRev(SelectedTexts, ",") - 1) & ", and " & _
Right(SelectedTexts, Len(SelectedTexts) - InStrRev(SelectedTexts, ",") - 1)
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = SelectedTexts
如果您不想要 'and' 前的逗号,请将 ", and " 更改为 " and "。
我有一个包含四个条目的列表框(多选)的用户表单。
以下代码构建字符串:
Dim myArray() As String
'Use Split function to return a zero based one dimensional array.
myArray = Split("text 1|" _
& "text 2|" _
& "text 3|" _
& "text 4", "|")
'Use .List method to populate listbox.
ListBox1.List = myArray
'Use the .ColumnWidth property to set column widths. 0 results in a hidden column.
ListBox1.ColumnWidths = "1"
lbl_Exit:
Exit Sub
下面的代码成功插入了四个选定条目的任意组合,每个条目之间带有 ,
和 space,包括末尾的 .
,即 text 1, .
或 text 1, text 2, text 3, .
或 text 1, text 2, text 3, text 4, .
到内容控件中。
Dim SelectedTexts As String
Dim index As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
'Adds comma after every entry
SelectedTexts = SelectedTexts & ListBox1.List(index) & ", "
End If
Next
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
Unload Me
lbl_Exit:
Exit Sub
Me.Repaint
Me.Hide
我正在努力完成两件事:
- 我只想在最后一个条目后添加一个
.
,即text 1.
而不是text 1, .
- 我希望在最后一个结果之前有一个
and
,即text 1 and text 2.
或text 1, text 2 and text 3.
或text 1, text 2 and text 4.
构建字符串后,您需要将其操作成您需要的形式。这是执行此操作的逻辑:
Private Sub Test()
Dim SelectedTexts As String
Dim index As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
SelectedTexts = SelectedTexts & ListBox1.List(index) & ", "
End If
Next
SelectedTexts = Mid(SelectedTexts, 1, Len(SelectedTexts) - 2) & "."
index = InStrRev(SelectedTexts, ",")
If index > 0 Then SelectedTexts = Left(SelectedTexts, index - 1) & " and " & Right(SelectedTexts, Len(SelectedTexts) - index - 1)
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = SelectedTexts
End Sub
编辑:调整大小写顺序
编辑 2:重构代码
Dim SelectedTexts As String
Dim index As Long
' Get total selected
Dim totalSelected As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
totalSelected = totalSelected + 1
End If
Next
' Convert total selected to base 0
totalSelected = totalSelected - 1
Dim counter As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
'Adds comma after every entry
Dim delimiter As String
Select Case True
Case counter = totalSelected - 1
delimiter = " and "
Case counter < totalSelected
delimiter = ", "
Case Else
delimiter = ""
End Select
SelectedTexts = SelectedTexts & ListBox1.List(index) & delimiter
counter = counter + 1
End If
Next
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
Unload Me
lbl_Exit:
Exit Sub
Me.Repaint
Me.Hide
只需更改:
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
至:
'Adds "and", plus period to the end
SelectedTexts = Left(SelectedTexts, Len(SelectedTexts) - 2) & "."
SelectedTexts = Left(SelectedTexts, InStrRev(SelectedTexts, ",") - 1) & ", and " & _
Right(SelectedTexts, Len(SelectedTexts) - InStrRev(SelectedTexts, ",") - 1)
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = SelectedTexts
如果您不想要 'and' 前的逗号,请将 ", and " 更改为 " and "。