Excel VBA:宏未识别循环中的For

时间:2017-07-14 15:44:27

标签: excel vba excel-vba word-vba

以下是我正在创建的宏,我在运行代码时遇到了一个问题。出于某种原因,当我运行它时,发生编译错误,说“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

1 个答案:

答案 0 :(得分:2)

您在以下代码中缺少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