You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 02:19:57