Excel VBA向Word填充列表异常:第二个列表混入所有数组内容
问题描述
我写了一段Excel VBA代码,将Excel工作簿中的数据提取到数组后,填充至Word文档内的多个列表中,每个列表对应唯一的占位符。6个列表中有5个可正常填充,但第二个列表总会混入所有数组的内容。调试打印显示BRArray数据正确,若调换数组执行顺序,问题始终出现在第二个执行的替换逻辑上。曾尝试将替换逻辑抽离为独立子过程,但未解决问题。
代码片段
' Loop through each row in ManagerArray For row = LBound(ManagerArray) To UBound(ManagerArray) ' Replace placeholder with ManagerArray row content wdoc.Content.Find.Execute FindText:="<<ManagerCriteria>>", ReplaceWith:=ManagerArray(row), Replace:=wdReplaceAll ' Insert new line and placeholder after the replaced content if it's not the last row If row < UBound(ManagerArray) Then wdoc.Content.Find.Execute FindText:=ManagerArray(row), ReplaceWith:=ManagerArray(row) & vbCr & "<<ManagerCriteria>>", Replace:=wdReplaceAll End If Next row ' Loop through each row in BRArray For row = LBound(BRArray) To UBound(BRArray) ' Replace placeholder with BRArray row content wdoc.Content.Find.Execute FindText:="<<BRCriteria>>", ReplaceWith:=BRArray(row), Replace:=wdReplaceAll ' Insert new line and placeholder after the replaced content if it's not the last row If row < UBound(BRArray) Then wdoc.Content.Find.Execute FindText:=BRArray(row), ReplaceWith:=BRArray(row) & vbCr & "<<BRCriteria>>", Replace:=wdReplaceAll End If Next row ' Loop through each row in AIMArray For row = LBound(AIMArray) To UBound(AIMArray) ' Replace placeholder with AIMArray row content wdoc.Content.Find.Execute FindText:="<<AIMCriteria>>", ReplaceWith:=AIMArray(row), Replace:=wdReplaceAll ' Insert new line and placeholder after the replaced content if it's not the last row If row < UBound(AIMArray) Then wdoc.Content.Find.Execute FindText:=AIMArray(row), ReplaceWith:=AIMArray(row) & vbCr & "<<AIMCriteria>>", Replace:=wdReplaceAll End If Next row ' Loop through each row in EISArray For row = LBound(EISArray) To UBound(EISArray) ' Replace placeholder with EISArray row content wdoc.Content.Find.Execute FindText:="<<EISCriteria>>", ReplaceWith:=EISArray(row), Replace:=wdReplaceAll ' Insert new line and placeholder after the replaced content if it's not the last row If row < UBound(EISArray) Then wdoc.Content.Find.Execute FindText:=EISArray(row), ReplaceWith:=EISArray(row) & vbCr & "<<EISCriteria>>", Replace:=wdReplaceAll End If Next row ' Loop through each row in SEISArray For row = LBound(SEISArray) To UBound(SEISArray) ' Replace placeholder with SEISArray row content wdoc.Content.Find.Execute FindText:="<<SEISCriteria>>", ReplaceWith:=SEISArray(row), Replace:=wdReplaceAll ' Insert new line and placeholder after the replaced content if it's not the last row If row < UBound(SEISArray) Then wdoc.Content.Find.Execute FindText:=SEISArray(row), ReplaceWith:=SEISArray(row) & vbCr & "<<SEISCriteria>>", Replace:=wdReplaceAll End If Next row ' Loop through each row in VCTArray For row = LBound(VCTArray) To UBound(VCTArray) ' Replace placeholder with VCTArray row content wdoc.Content.Find.Execute FindText:="<<VCTCriteria>>", ReplaceWith:=VCTArray(row), Replace:=wdReplaceAll ' Insert new line and placeholder after the replaced content if it's not the last row If row < UBound(VCTArray) Then wdoc.Content.Find.Execute FindText:=VCTArray(row), ReplaceWith:=VCTArray(row) & vbCr & "<<VCTCriteria>>", Replace:=wdReplaceAll End If Next row
内容的提问来源于stack exchange,提问作者Van Li
相关产品推荐
相关产品推荐

