使用 Excel VBA 循环影响代码输出
Loop Impacting Code Output using Excel VBA
我想知道是否有人可以帮我调试我的代码。我似乎无法弄清楚为什么当循环移动到第二行时,它会改变数组中的元素。
例如:如果数组 "BookMarksToDelete" 中有 "Dog, Cat, Bird",则第二个循环似乎想使其成为下一行 "og, Cat, Bird"。
是否有更好的循环方法?
For Each rw In myRange.Rows
For Each cel In rw.Cells
For i = LBound(myArray) To UBound(myArray)
Set oCell = myRange.Find(What:=myArray(i), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If oCell Is Nothing Then
sTemp = sTemp & "," & myArray(i)
Else
Set oCell = Nothing
End If
Next i
sTemp = Mid(sTemp, 2)
If Not Len(Trim(sTemp)) = 0 Then
BookMarksToDelete = Split(sTemp, ",")
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(FlName)
For i = LBound(BookMarksToDelete) To UBound(BookMarksToDelete)
Set pRng = wdDoc.Bookmarks(BookMarksToDelete(i)).Range
pRng.MoveEnd wdParagraph, 2
pRng.Delete
Next i
End If
Set wdTable = wdDoc.Tables(1)
For i = LBound(BookMarksToDelete) To UBound(BookMarksToDelete)
For Each cell In myRangeRef
If InStr(1, cell.Value, BookMarksToDelete(i), vbTextCompare) Then
aCell = cell.Offset(, -1).Value
stTemp = stTemp & "," & aCell
End If
Next cell
Next i
stTemp = Mid(stTemp, 2)
If Not Len(Trim(stTemp)) = 0 Then
ReturnsToDelete = Split(stTemp, ",")
For i = LBound(ReturnsToDelete) To UBound(ReturnsToDelete)
For j = wdTable.Rows.Count To 2 Step -1
If Left(wdTable.cell(j, 1).Range.Text, Len(wdTable.cell(j, 1).Range.Text) - 2) = ReturnsToDelete(i) Then wdTable.Rows(j).Delete
Next j
Next i
End If
With ThisWorkbook.Sheets("Investors Database")
firstName = .Range("B" & rw)
lastName = .Range("A" & rw)
titleName = lastName & ", " & firstName
End With
Set tRng = wdDoc.Bookmarks("TitlePageName").Range
tRng.Text = wdDoc.Bookmarks("TitlePageName").Range.Text & titleName
wdDoc.Bookmarks.Add "TitlePageName", tRng
d = "Information Up-To-Date as of " & Date
Set dRng = wdDoc.Bookmarks("TitlePageDate").Range
dRng.Text = wdDoc.Bookmarks("TitlePageDate").Range.Text & d
wdDoc.Bookmarks.Add "TitlePageDate", dRng
Set wRng = wdApp.ActiveDocument.Bookmarks("FundCommentary").Range
wRng.Collapse wdCollapseStart
wRng.InsertBreak wdPageBreak
Set sRng = wdApp.ActiveDocument.Bookmarks("Disclaimer").Range
sRng.Collapse wdCollapseStart
sRng.InsertBreak wdPageBreak
wdDoc.TablesOfContents(1).Update
wdDoc.Repaginate
With wdApp.ActiveDocument
.SaveAs2 "https://websitefolder//document.docx"
.Close
End With
wdApp.Visible = False
Next
Next
For example: if the array "BookMarksToDelete" has Dog, Cat, Bird
in it, the second loop seem to want to make it og, Cat, Bird
in the next row.
循环第一次运行时,sTemp
的值为""
,所以sTemp = sTemp & "," & myArray(i)
在开头加了一个,
,然后sTemp = Mid(sTemp, 2)
从头开始修剪 ,
。
当代码第二次循环时,sTemp
的值不是 ""
,而是下一行的代码 sTemp = Mid(sTemp, 2)
第一个字母的印章。
选项 1:重置 sTemp
在代码末尾添加行 sTemp = ""
选项 2:如果您不想重置 sTemp
,请检查第一个字符是否为 ,
或
将sTemp = Mid(sTemp, 2)
替换为If Left(sTemp, 1) = "," Then sTemp = Mid(sTemp, 2)
我想知道是否有人可以帮我调试我的代码。我似乎无法弄清楚为什么当循环移动到第二行时,它会改变数组中的元素。
例如:如果数组 "BookMarksToDelete" 中有 "Dog, Cat, Bird",则第二个循环似乎想使其成为下一行 "og, Cat, Bird"。
是否有更好的循环方法?
For Each rw In myRange.Rows
For Each cel In rw.Cells
For i = LBound(myArray) To UBound(myArray)
Set oCell = myRange.Find(What:=myArray(i), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If oCell Is Nothing Then
sTemp = sTemp & "," & myArray(i)
Else
Set oCell = Nothing
End If
Next i
sTemp = Mid(sTemp, 2)
If Not Len(Trim(sTemp)) = 0 Then
BookMarksToDelete = Split(sTemp, ",")
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(FlName)
For i = LBound(BookMarksToDelete) To UBound(BookMarksToDelete)
Set pRng = wdDoc.Bookmarks(BookMarksToDelete(i)).Range
pRng.MoveEnd wdParagraph, 2
pRng.Delete
Next i
End If
Set wdTable = wdDoc.Tables(1)
For i = LBound(BookMarksToDelete) To UBound(BookMarksToDelete)
For Each cell In myRangeRef
If InStr(1, cell.Value, BookMarksToDelete(i), vbTextCompare) Then
aCell = cell.Offset(, -1).Value
stTemp = stTemp & "," & aCell
End If
Next cell
Next i
stTemp = Mid(stTemp, 2)
If Not Len(Trim(stTemp)) = 0 Then
ReturnsToDelete = Split(stTemp, ",")
For i = LBound(ReturnsToDelete) To UBound(ReturnsToDelete)
For j = wdTable.Rows.Count To 2 Step -1
If Left(wdTable.cell(j, 1).Range.Text, Len(wdTable.cell(j, 1).Range.Text) - 2) = ReturnsToDelete(i) Then wdTable.Rows(j).Delete
Next j
Next i
End If
With ThisWorkbook.Sheets("Investors Database")
firstName = .Range("B" & rw)
lastName = .Range("A" & rw)
titleName = lastName & ", " & firstName
End With
Set tRng = wdDoc.Bookmarks("TitlePageName").Range
tRng.Text = wdDoc.Bookmarks("TitlePageName").Range.Text & titleName
wdDoc.Bookmarks.Add "TitlePageName", tRng
d = "Information Up-To-Date as of " & Date
Set dRng = wdDoc.Bookmarks("TitlePageDate").Range
dRng.Text = wdDoc.Bookmarks("TitlePageDate").Range.Text & d
wdDoc.Bookmarks.Add "TitlePageDate", dRng
Set wRng = wdApp.ActiveDocument.Bookmarks("FundCommentary").Range
wRng.Collapse wdCollapseStart
wRng.InsertBreak wdPageBreak
Set sRng = wdApp.ActiveDocument.Bookmarks("Disclaimer").Range
sRng.Collapse wdCollapseStart
sRng.InsertBreak wdPageBreak
wdDoc.TablesOfContents(1).Update
wdDoc.Repaginate
With wdApp.ActiveDocument
.SaveAs2 "https://websitefolder//document.docx"
.Close
End With
wdApp.Visible = False
Next
Next
For example: if the array "BookMarksToDelete" has
Dog, Cat, Bird
in it, the second loop seem to want to make itog, Cat, Bird
in the next row.
循环第一次运行时,sTemp
的值为""
,所以sTemp = sTemp & "," & myArray(i)
在开头加了一个,
,然后sTemp = Mid(sTemp, 2)
从头开始修剪 ,
。
当代码第二次循环时,sTemp
的值不是 ""
,而是下一行的代码 sTemp = Mid(sTemp, 2)
第一个字母的印章。
选项 1:重置 sTemp
在代码末尾添加行 sTemp = ""
选项 2:如果您不想重置 sTemp
,请检查第一个字符是否为 ,
或
将sTemp = Mid(sTemp, 2)
替换为If Left(sTemp, 1) = "," Then sTemp = Mid(sTemp, 2)