标准但特定的“不使用With结束”编译错误

时间:2019-09-09 14:37:53

标签: excel vba

给出一个标准的编译错误“不带With结束” msgbox。 我刚刚开始vba编码。请给我一些指导,让我向前迈进。 (由于插入了代码,制表符出错了。不要为此烦恼。)

我检查了IF语句,对象正确性和包含的循环。

'*2/B. - Generate template based new excel file and fill up cells with data of document attributes*
    For n = 1 To documentCount
    Dim strTemplate As String: strTemplate = "C:\Users\C3642\Desktop\FU5504-Elfogadhatosagi_Nyilatkozat_formanyomtatvany\PA2-FU-5504-NY-01_v2.xlsx"
    Set outXl = xlApp.Workbooks.Open(strTemplate, True)
    Set outXl = ActiveWorkbook
    With outXl.Worksheets(1)
    .SaveAs Filename:="C:\Users\C3642\Desktop\FU5504-Elfogadhatosagi_Nyilatkozat_formanyomtatvany\" & docCode(n) & "_" & docResponsible(n) & ".xlsx", FileFormat:=xlOpenXMLWorkbookMacroEnabled
    .Cells(5, 4).Value = docName(n)
    .Cells(6, 4).Value = docCode(n)
    '.Cells(6, 13).Value = docRevision(n) <- nincs hozzá adat :(
    .Cells(14, 4).Value = docResponsible(n)
    If departmentHead(n) = "Name1" Then
            .Cells(14, 7).Value = "VIO"
            .Cells(31, 7).Value = "VIO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name2" Then
            .Cells(14, 7).Value = "ITO"
            .Cells(31, 7).Value = "ITO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name3" Then
            .Cells(14, 7).Value = "VSKO"
            .Cells(31, 7).Value = "VSKO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name4" Then
            .Cells(14, 7).Value = "AMO"
            .Cells(31, 7).Value = "AMO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name5" Then
            .Cells(14, 7).Value = "NUO"
            .Cells(31, 7).Value = "NUO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name6" Then
            .Cells(14, 7).Value = "KGO"
            .Cells(31, 7).Value = "KGO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name7" Then
            .Cells(14, 7).Value = "PGO"
            .Cells(31, 7).Value = "PGO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name8" Then
            .Cells(14, 7).Value = "ETO"
            .Cells(31, 7).Value = "ETO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name9" Then
            .Cells(14, 7).Value = "GMDO"
            .Cells(31, 7).Value = "GMDO"
            .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name10" Then
            .Cells(14, 7).Value = "ABO"
            .Cells(31, 7).Value = "ABO"
            .Cells(31, 4).Value = departmentHead(n)
    Else
            .Cells(14, 7).Value = ""
            .Cells(31, 7).Value = ""
            .Cells(31, 4).Value = ""
    '.Cells(31, 4).Value = supervisorName(n)
    '.Cells(31, 7).Value = supervisorDepartment(n)
    .Cells(33, 4).Value = "Name0"
    Dim innerReviewers() As String: Dim split_ETO_reviewers() As String: Dim split_KGO_reviewers() As String: Dim split_PGO_reviewers() As String: Dim split_NUO_reviewers() As String
    Dim split_AMO_reviewers() As String: Dim split_VSKO_reviewers() As String: Dim split_VIO_reviewers() As String: Dim split_ABO_reviewers() As String: Dim split_ITO_reviewers() As String
    Dim split_UIG_reviewers() As String: Dim split_ENBO_reviewers() As String: Dim split_LETO_reviewers() As String: Dim split_Non_ERBE_reviewers() As String: Dim split_ERBE_reviewers() As String
    innerReviewers() = Split(reviewers(n), ",")
    split_ETO_reviewers() = Split(ETO_reviewers(n), ",")
    split_KGO_reviewers() = Split(KGO_reviewers(n), ",")
    split_PGO_reviewers() = Split(PGO_reviewers(n), ",")
    split_NUO_reviewers() = Split(NUO_reviewers(n), ",")
    split_AMO_reviewers() = Split(AMO_reviewers(n), ",")
    split_VSKO_reviewers() = Split(VSKO_reviewers(n), ",")
    split_VIO_reviewers() = Split(VIO_reviewers(n), ",")
    split_ABO_reviewers() = Split(ABO_reviewers(n), ",")
    split_ITO_reviewers() = Split(ITO_reviewers(n), ",")
    split_UIG_reviewers() = Split(UIG_reviewers(n), ",")
    split_ENBO_reviewers() = Split(ENBO_reviewers(n), ",")
    split_LETO_reviewers() = Split(LETO_reviewers(n), ",")
    split_Non_ERBE_reviewers() = Split(Non_ERBE_reviewers(n), ",")
    split_ERBE_reviewers() = Split(ERBE_reviewers(n), ",")
    reviewersCount = 0
    If IsEmpty(innerReviewers) Or (IsEmpty(innerReviewers) & IsEmpty(split_ETO_reviewers) & IsEmpty(split_KGO_reviewers) & IsEmpty(split_PGO_reviewers) & IsEmpty(split_NUO_reviewers) & IsEmpty(split_AMO_reviewers) & IsEmpty(split_VSKO_reviewers) & IsEmpty(split_VIO_reviewers) & IsEmpty(split_ABO_reviewers) & IsEmpty(split_ITO_reviewers)) Then
        MsgBox "The current document has not been reviewed by colleques! (Archiving function of list of non-reviewed docs is not ready.)", vbOKOnly, "Warning for lacking reviewers!"
    Else
        reviewersCount = UBound(innerReviewers) - LBound(innerReviewers) + 1
        Dim i As Integer
        i = 0
        Do While (i < reviewersCount & i < 12)
            .Cells(15 + i, 4).Value = innerReviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_ETO_reviewers) - LBound(split_ETO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = ETO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_KGO_reviewers) - LBound(split_KGO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = KGO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_PGO_reviewers) - LBound(split_PGO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = PGO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_NUO_reviewers) - LBound(split_NUO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = NUO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_AMO_reviewers) - LBound(split_AMO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = AMO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_VSKO_reviewers) - LBound(split_VSKO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = VSKO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_VIO_reviewers) - LBound(split_VIO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = VIO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_ABO_reviewers) - LBound(split_ABO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = ABO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_ITO_reviewers) - LBound(split_ITO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = ITO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_UIG_reviewers) - LBound(split_UIG_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = UIG_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_ENBO_reviewers) - LBound(split_ENBO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = ENBO_reviewers(i)
            i = i + 1
        Loop
        reviewersCount = reviewersCount + UBound(split_LETO_reviewers) - LBound(split_LETO_reviewers) + 1
        Do While (i < (12 - reviewersCount))
            .Cells(15 + i, 4).Value = LETO_reviewers(i)
            i = i + 1
        Loop
        'reviewersCount = reviewersCount + UBound(split_Non_ERBE_reviewers) - LBound(split_Non_ERBE_reviewers) + 1
        'Do While (i < (12 - reviewersCount))
        '     .Cells(15 + i, 4).Value = Non_ERBE_reviewers(i)
        '     i = i + 1
        'Loop
        'reviewersCount = reviewersCount + UBound(split_ERBE_reviewers) - LBound(split_ERBE_reviewers) + 1
        'Do While (i < (12 - reviewersCount))
        '     .Cells(15 + i, 4).Value = ERBE_reviewers(i)
        '     i = i + 1
        'Loop
    End With
    outXl.Close SaveChanges:=True
    Next n

也许Workbook对象未正确设置(但在我的代码中声明了该对象)。

3 个答案:

答案 0 :(得分:3)

显示错误,因为到处都缺少End If

例如:

If departmentHead(n) = "Name3" Then
        .Cells(14, 7).Value = "VSKO"
        .Cells(31, 7).Value = "VSKO"
        .Cells(31, 4).Value = departmentHead(n)
End If

答案 1 :(得分:3)

期望有一堆End If或ElseIfs,而没有看到它们是在抱怨第一个未封闭的语句(with)。

例如,将其修复为:

files = dir(fullfile(workdir, '*', 'plane.txt'));
for ii=1:numel(files)
   file = fullfile(files(ii).folder, files(ii).name);
   plane_data = importdata(file);
   % do something with plane_data here...
end

答案 2 :(得分:3)

  

插入代码后,制表符出错。不必为此烦恼。

但是,通过indenter运行代码(免责声明:这是Rubberduck Smart Indenter端口; Rubberduck是我管理的免费和开源VBIDE外接程序项目),它为您提供了主要线索:

If departmentHead(n) = "Name1" Then
    .Cells(14, 7).Value = "VIO"
    .Cells(31, 7).Value = "VIO"
    .Cells(31, 4).Value = departmentHead(n)
    If departmentHead(n) = "Name2" Then
        .Cells(14, 7).Value = "ITO"
        .Cells(31, 7).Value = "ITO"
        .Cells(31, 4).Value = departmentHead(n)
        If departmentHead(n) = "Name3" Then
            .Cells(14, 7).Value = "VSKO"
            .Cells(31, 7).Value = "VSKO"
            .Cells(31, 4).Value = departmentHead(n)
            If departmentHead(n) = "Name4" Then
                .Cells(14, 7).Value = "AMO"
                .Cells(31, 7).Value = "AMO"
                .Cells(31, 4).Value = departmentHead(n)
                If departmentHead(n) = "Name5" Then
                    .Cells(14, 7).Value = "NUO"
                    .Cells(31, 7).Value = "NUO"
                    .Cells(31, 4).Value = departmentHead(n)
                    If departmentHead(n) = "Name6" Then
                        .Cells(14, 7).Value = "KGO"
                        .Cells(31, 7).Value = "KGO"
                        .Cells(31, 4).Value = departmentHead(n)
                        If departmentHead(n) = "Name7" Then
                            .Cells(14, 7).Value = "PGO"
                            .Cells(31, 7).Value = "PGO"
                            .Cells(31, 4).Value = departmentHead(n)
                            If departmentHead(n) = "Name8" Then
                                .Cells(14, 7).Value = "ETO"
                                .Cells(31, 7).Value = "ETO"
                                .Cells(31, 4).Value = departmentHead(n)
                                If departmentHead(n) = "Name9" Then
                                    .Cells(14, 7).Value = "GMDO"
                                    .Cells(31, 7).Value = "GMDO"
                                    .Cells(31, 4).Value = departmentHead(n)
                                    If departmentHead(n) = "Name10" Then
                                        .Cells(14, 7).Value = "ABO"
                                        .Cells(31, 7).Value = "ABO"
                                        .Cells(31, 4).Value = departmentHead(n)
                                    Else
                                        .Cells(14, 7).Value = ""
                                        .Cells(31, 7).Value = ""
                                        .Cells(31, 4).Value = ""

所有这些If块实际上都是嵌套,因为它们从未以End If令牌终止。因此,当编译器到达End With令牌时,它不知道要针对哪个令牌,因为它仍然希望关闭最里面的If块。


经验法则,当您重复所有条件都评估同一个值时,您需要一个Select...Case块-还可以考虑通过将变化的字符串值提取到其自己的局部变量中来减少冗余:

Dim result As String
Select Case departmentHead(n)
    Case "Name1"
        result = "VIO"
    Case "Name2"
        result = "ITO"
    Case "Name3"
        result = "VSKO"
    '...
End Select

.Cells(14, 7).Value = result
.Cells(31, 7).Value = result
If result <> vbNullString Then .Cells(31, 4) = departmentHead(n)