VBA:从数组中创建选定的字符串
VBA: Create selected string from an array
编辑:在此先感谢您的帮助。我想遍历一个数组并创建一个基于直到不再满足条件的字符串。我认为我当前的代码创建了一个无限循环。
我在数组中有以下内容(小节以“-”开头)。注意,请忽略引号 - 项目符号的破折号格式,因此必须将它们放入以阻止它。
"- 第 2 小节"
"- 第 3 小节"
"Second Section"
"- 第 4 小节"
"Third Section"
"- 第 5 小节"
我想创建一个新字符串来存储 仅 那些 strings/array 以“-”开头的插槽,直到并排除下一个不以“-”开头的插槽“-”。我想要的结果字符串是:
"- 第 1 小节"
"- 第 2 小节"
“- 第 3 小节”
(不包括“- 第 4 款”和“- 第 5 款”)
本质上,我想要同一 'master' 部分中的其余小节。
下面是我的尝试:
Dim testArray() As Variant
Dim count1 As Integer
Dim CurrentSectionIndex as Integer
CurrentSectionIndex = ActivePresentation.Slides(i).sectionIndex
count1 = ActivePresentation.SectionProperties.Count - CurrentSectionIndex
'clear previous array (I am looping through slides)
Erase testArray
' size the array
ReDim testArray(1 To count1)
'Fill the array
For n = 1 To count1
testArray(n) = ActivePresentation.SectionProperties.Name(CurrentSectionIndex + n)
Next n
Dim AllPostSections As String
Dim PostSections As String
For m = LBound(testArray) To UBound(testArray)
Do While testArray(m) Like "-*"
PostSections = testArray(m)
Loop
AllPostSections = AllPostSections & PostSections & vbNewLine
Next m
如有任何帮助,我们将不胜感激!
你关于为什么要使用 Do While/Until 循环的基本假设是正确的,但你的实现是不正确的。对于您尝试实现的任务,没有必要使用 Do While 循环,因为您需要的循环是 For M 循环。然后在 For 循环中,您需要做的就是使用 If 语句测试 assemble 您想要的字符串。如果您愿意,可以将 for next 循环替换为 Do while 循环,但实际上使用 collections 更容易实现您想要的效果。您可以使用与数组相同的方式对 collection 中的项目进行索引,因此除非您查看变量定义,否则您可以判断 my_array(1) 使用的是数组还是 collection。
下面的代码将获取所有标题(测试数组)的 collection 并生成一个新的 collection,其中您只有串联的副标题。
Option Explicit
Dim my_headings As Collection
Dim my_heading As Variant
Dim my_subheadings As Collection
Dim my_collector As String
my_collector = vbNullString
' fill the my_heading collection
Set my_subheadings = New Collection
For Each my_heading In my_headings
If my_heading Like "-*" Then
my_collector = my_collector & my_heading & vbCrLf
Else
If Len(my_collector) > 0 Then
my_sub_headings.Add my_collector
my_collector = vbNullString
End If
End If
Next
已编辑:这里的逻辑与上面相同,但 for next 循环替换为 do while 循环。希望这会阐明您对 do While 循环的作用相当困惑的想法。
Dim my_headings As Collection
Dim my_subheadings As Collection
Dim my_collector As String
Dim my_index As Long
my_collector = vbNullString
' fill the my_heading collection
Set my_subheadings = New Collection
my_index = 1
Do While my_index <= my_headings.Count
If my_headings(my_index) Like "-*" Then
my_collector = my_collector & my_heading & vbCrLf
Else
If Len(my_collector) > 0 Then
my_sub_headings.Add my_collector
my_collector = vbNullString
End If
End If
my_index=my_index+1
Loop
编辑:在此先感谢您的帮助。我想遍历一个数组并创建一个基于直到不再满足条件的字符串。我认为我当前的代码创建了一个无限循环。
我在数组中有以下内容(小节以“-”开头)。注意,请忽略引号 - 项目符号的破折号格式,因此必须将它们放入以阻止它。
"- 第 2 小节"
"- 第 3 小节"
"Second Section"
"- 第 4 小节"
"Third Section"
"- 第 5 小节"
我想创建一个新字符串来存储 仅 那些 strings/array 以“-”开头的插槽,直到并排除下一个不以“-”开头的插槽“-”。我想要的结果字符串是:
"- 第 1 小节"
"- 第 2 小节"
“- 第 3 小节”
(不包括“- 第 4 款”和“- 第 5 款”)
本质上,我想要同一 'master' 部分中的其余小节。
下面是我的尝试:
Dim testArray() As Variant
Dim count1 As Integer
Dim CurrentSectionIndex as Integer
CurrentSectionIndex = ActivePresentation.Slides(i).sectionIndex
count1 = ActivePresentation.SectionProperties.Count - CurrentSectionIndex
'clear previous array (I am looping through slides)
Erase testArray
' size the array
ReDim testArray(1 To count1)
'Fill the array
For n = 1 To count1
testArray(n) = ActivePresentation.SectionProperties.Name(CurrentSectionIndex + n)
Next n
Dim AllPostSections As String
Dim PostSections As String
For m = LBound(testArray) To UBound(testArray)
Do While testArray(m) Like "-*"
PostSections = testArray(m)
Loop
AllPostSections = AllPostSections & PostSections & vbNewLine
Next m
如有任何帮助,我们将不胜感激!
你关于为什么要使用 Do While/Until 循环的基本假设是正确的,但你的实现是不正确的。对于您尝试实现的任务,没有必要使用 Do While 循环,因为您需要的循环是 For M 循环。然后在 For 循环中,您需要做的就是使用 If 语句测试 assemble 您想要的字符串。如果您愿意,可以将 for next 循环替换为 Do while 循环,但实际上使用 collections 更容易实现您想要的效果。您可以使用与数组相同的方式对 collection 中的项目进行索引,因此除非您查看变量定义,否则您可以判断 my_array(1) 使用的是数组还是 collection。
下面的代码将获取所有标题(测试数组)的 collection 并生成一个新的 collection,其中您只有串联的副标题。
Option Explicit
Dim my_headings As Collection
Dim my_heading As Variant
Dim my_subheadings As Collection
Dim my_collector As String
my_collector = vbNullString
' fill the my_heading collection
Set my_subheadings = New Collection
For Each my_heading In my_headings
If my_heading Like "-*" Then
my_collector = my_collector & my_heading & vbCrLf
Else
If Len(my_collector) > 0 Then
my_sub_headings.Add my_collector
my_collector = vbNullString
End If
End If
Next
已编辑:这里的逻辑与上面相同,但 for next 循环替换为 do while 循环。希望这会阐明您对 do While 循环的作用相当困惑的想法。
Dim my_headings As Collection
Dim my_subheadings As Collection
Dim my_collector As String
Dim my_index As Long
my_collector = vbNullString
' fill the my_heading collection
Set my_subheadings = New Collection
my_index = 1
Do While my_index <= my_headings.Count
If my_headings(my_index) Like "-*" Then
my_collector = my_collector & my_heading & vbCrLf
Else
If Len(my_collector) > 0 Then
my_sub_headings.Add my_collector
my_collector = vbNullString
End If
End If
my_index=my_index+1
Loop