光标下的标识符无法识别-Excel宏

时间:2019-01-04 16:47:29

标签: excel vba identifier

我有一个宏正在运行以更新Excel中的报表表,但是在此特定代码行上我不断收到错误:

totalFieldsStart = Left(totalFieldsStart, Len(totalFieldsStart) - 1) & ")"

这里是整个宏的外观:

Function AppnSOFFormulasState(fTL As Range, fBR As Range)
    Dim final As Worksheet

    Dim aCol As Integer                          'index for appn column
    Dim dCol As Integer                          'index for div column
    Dim mCol As Integer                          'index for mdep column

    Dim appn As String
    Dim st As String
    Dim div As String
    Dim mdep As String
    Dim stateAdd As String
    Dim ntlAdd As String
    Dim totalFieldsStart As String               'cells to total will be separated
    Dim totalFieldsAFP As String                 'cells to total will be separated
    Dim totalFieldsOBS As String                 'cells to total will be separated
    Dim subFields As Range                       'cells to subtotal will be contiguous
    Dim c As Range                               'cell iterator

    Set final = ThisWorkbook.Worksheets(1)
    final.Activate
    aCol = final.Range("A1").Column
    dCol = final.Range("B1").Column
    mCol = final.Range("C1").Column

    'set top left to be first October cell for APPN
    appn = final.Cells(fTL.Row, aCol)

    st = Range("state_select").address

    totalFieldsAFP = "=sum("
    totalFieldsOBS = "=sum("

    Set fTL = fTL.Offset(0, 3)

    For Each c In final.Range(fTL, final.Cells(fBR.Offset(-1, 0).Row, fTL.Column))
        If Not IsEmpty(final.Cells(c.Row, dCol)) Then
            'the first line will have nothing for div, so the range part of the next if will fail
            On Error GoTo SkipFirst
            If final.Cells(c.Row, dCol) = final.Range(div) & sTotal Then
                c.Formula = "=sum(" & subFields.address & ":" & c.Offset(-1, 0).address & ")"
                c.Offset(0, 1).Formula = "=sum(" & subFields.Offset(0, 1).address & ":" & c.Offset(-1, 1).address & ")"
                totalFieldsAFP = totalFieldsAFP & c.address & ", "
                totalFieldsOBS = totalFieldsOBS & c.Offset(0, 1).address & ", "
            Else
                SkipFirst:
                On Error GoTo 0
                Set subFields = c
                div = final.Cells(c.Row, dCol).address
            End If
        End If
        If Not IsEmpty(final.Cells(c.Row, mCol)) Then
            mdep = final.Cells(c.Row, mCol).address

            stateAdd = "left(" & st & ",2) &" & appn & "&" & div & "&" & mdep
            ntlAdd = appn & "&" & div & "&" & mdep

            'AFP
            c.Formula = "=iferror(VLOOKUP(" & stateAdd & ",state_lookup_sof,3,FALSE),0)"
            'Obs
            c.Offset(0, 1).Formula = "=iferror(VLOOKUP(" & stateAdd & ",state_lookup_sof,4,FALSE),0)"
        End If
    Next c

    totalFieldsStart = Left(totalFieldsStart, Len(totalFieldsStart) - 1) & ")"
    totalFieldsAFP = Left(totalFieldsAFP, Len(totalFieldsAFP) - 1) & ")"
    totalFieldsOBS = Left(totalFieldsOBS, Len(totalFieldsOBS) - 1) & ")"

    final.Cells(fBR.Row, fTL.Column).Formula = totalFieldsAFP
    final.Cells(fBR.Row, fTL.Offset(0, 1).Column).Formula = totalFieldsOBS
End Function

0 个答案:

没有答案