I have a code snippet that pulls the data from my Excel workbook into an array and then populates the list within a Word document. There are multiple lists within the document and each has a unique placeholder. The code runs perfectly well for 5 out of 6 lists, but for some reason mashes all arrays together into the 2nd list. A snippet of the code below:
' 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
The codes are all the same, except for the placeholder and corresponding array. The issue occurs with the BRArray replacement – I have debug printed it and it is showing the correct list in the immediate window. If I move any of the following arrays around, it will always happen to whatever replacement happens 2nd in the code. Any ideas on what could the issue be?
Tried separating the replacement part into a separate subroutine, but this did not solve the issue.
Van Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.