Excel VBA:宏无法识别循环中的 For
Posted
技术标签:
【中文标题】Excel VBA:宏无法识别循环中的 For【英文标题】:Excel VBA: Macro not Identifying the For in the Loop 【发布时间】:2017-07-14 15:44:26 【问题描述】:以下是我正在创建的宏,我在运行代码时遇到了问题。由于某种原因,当我运行它时出现编译错误,说“Next without For”,因为它在代码末尾识别“Next l”,但它没有识别“For l = 7 To LastRow”中的第一行。有谁知道为什么会出现这个编译错误?
For l = 7 To LastRow
For i = 3 To LastColumn
If sht.Cells(l, i).Value = "" Then
V = sht.Cells(6, i).Value
sTemp = sTemp & "," & V
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("Client Database")
firstName = .Range("B" & l)
lastName = .Range("A" & l)
If firstName = Chr(13) & Chr(7) Then
titleName = lastName
Else
titleName = lastName & ", " & firstName
End If
End With
Set tRng = wdDoc.Bookmarks("TitlePageName").Range
tRng.Text = wdDoc.Bookmarks("TitlePageName").Range.Text & titleName
wdDoc.Bookmarks.Add "TitlePageName", tRng
If Date <= 31 / 3 / 2017 Then
quartDate = "March 31" & ", " & Year(Date)
ElseIf 31 / 3 / 2017 <= Date <= 30 / 6 / 2017 Then
quartDate = "June 30" & ", " & Year(Date)
ElseIf 30 / 6 / 2017 <= Date <= 30 / 9 / 2017 Then
quartDate = "September 30" & ", " & Year(Date)
Else
quartDate = "December 31" & ", " & Year(Date)
d = "Information as of" & " " & quartDate
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
Set mRng = wdApp.ActiveDocument.Bookmarks("MonitoringChecklist").Range
mRng.Collapse wdCollapseStart
mRng.InsertBreak wdPageBreak
myArray = Array("CreditMon", "UncorrelatedMon", "FixedMon")
myArray2 = Array("CreditMon2", "UncorrelatedMon2", "FixedMon2")
LastTable = wdDoc.Tables.Count
t = 3
Do Until t = LastTable + 1
Set wdTable = wdDoc.Tables(t)
For i = LBound(ReturnsToDelete) To UBound(ReturnsToDelete)
For j = wdTable.Columns.Count To 2 Step -1
If Left(wdTable.cell(1, j).Range.Text, Len(wdTable.cell(1, j).Range.Text) - 2) = ReturnsToDelete(i) Then wdTable.Columns(j).Delete
Next j
Next i
If wdTable.Columns.Count = 1 Then
wdTable.Delete
t = t
LastTable = wdDoc.Tables.Count
Else
wdTable.Rows.Alignment = wdAlignRowLeft
t = t + 1
End If
Loop
For i = 0 To 2
If wdDoc.Bookmarks.Exists(myArray2(i)) = False Then
wdDoc.Bookmarks(myArray(i)).Range.Delete
Else
wdDoc.Bookmarks(myArray(i)).Range.Collapse wdCollapseStart
wdDoc.Bookmarks(myArray(i)).Range.InsertBreak wdPageBreak
wdDoc.TablesOfContents(1).Update
wdDoc.Repaginate
shName = "PQ Quarterly Reporting for " & firstName & " " & lastName
With wdApp.ActiveDocument
.SaveAs2 "https://path/" & shName & ".docx"
.Close
End With
wdApp.Visible = False
sTemp = ""
stTemp = ""
Next l
【问题讨论】:
您在定义quartDate
后缺少 End If
。
您的 quartdate
If 子句缺少 End If。其他地方可能存在类似的问题。
这里有几个错误。 Finjo 和 Brian 都是对的。您还缺少“for I = 0 To 2”循环中的 endif
讽刺,因为你对缩进的使用很好,这是发现这些问题的最好方法。
【参考方案1】:
您在以下代码中缺少 Next 语句:
For i = 0 To 2
If wdDoc.Bookmarks.Exists(myArray2(i)) = False Then
wdDoc.Bookmarks(myArray(i)).Range.Delete
Else
wdDoc.Bookmarks(myArray(i)).Range.Collapse wdCollapseStart
wdDoc.Bookmarks(myArray(i)).Range.InsertBreak wdPageBreak
【讨论】:
以上是关于Excel VBA:宏无法识别循环中的 For的主要内容,如果未能解决你的问题,请参考以下文章