Excel VBA-If语句上的运行时错误'1004'

时间:2018-09-11 19:29:21

标签: excel vba excel-vba

我正在制作一个VBA程序,该程序可以更改excel文件的外观。有一些标签(“ BN”,“ A”,“ C”等)说明应如何更改行/单元格。

例如:标签“ A”的意思是-将单元格字体设置为“ Arial”,大小为13 ...等。

该程序一直有效,直到我不久前进行了一些更改。从那时起,它总是给我一个运行时错误。有谁知道为什么吗?

代码:

Option Explicit

Sub macro1()

Dim rowIndex As Integer
Dim lastRowIndex As Integer
Dim offset As Integer


lastRowIndex = 2700

With ActiveSheet
    For rowIndex = 1 To 3
        Rows(1).EntireRow.Delete
    Next rowIndex

    With Cells.Font
        .Name = "Arial"
        .Size = 8
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Bold = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = 1
    End With
    With Cells
        .RowHeight = 11
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

    Columns(1).ColumnWidth = 13.5
    Columns(2).ColumnWidth = 60
    With Columns(3)
        .HorizontalAlignment = xlCenter
        .ColumnWidth = 5.5
    End With
    With Columns(4)
        .HorizontalAlignment = xlRight
        .ColumnWidth = 6.5
    End With
    With Columns(5)
        .HorizontalAlignment = xlRight
        .ColumnWidth = 6.5
    End With


    Columns(4).HorizontalAlignment = xlRight
    Columns(5).HorizontalAlignment = xlRight

    rowIndex = 1
    offset = 0

    Do While (rowIndex - offset) < lastRowIndex
        If Cells(rowIndex, 5).Value = "A" Or Cells(rowIndex, 5).Value = "NAZOV" Or _
            Cells(rowIndex, 5).Value = "C" Or Cells(rowIndex, 6).Value = "BN" Then

            If Cells(rowIndex, 5).Value = "A" Then
                Cells(rowIndex, 5).ClearContents
                With Cells(rowIndex, 2).Font
                    .Name = "Arial Narrow"
                    .Size = 11
                    .Bold = True
                    .Color = RGB(204, 0, 0)
                End With
                With Cells(rowIndex, 2)
                    .RowHeight = 16
                    .HorizontalAlignment = xlCenter
                End With
            End If

            If Cells(rowIndex, 5).Value = "NAZOV" Then
                Cells(rowIndex, 5).ClearContents
                With Cells(rowIndex, 2).Font
                    .Name = "Arial"
                    .Size = 9
                    .Bold = True
                    .Underline = xlUnderlineStyleSingle
                    .Color = RGB(0, 0, 153)
                End With
                With Cells(rowIndex, 2)
                    .RowHeight = 13
                End With
            End If

            If Cells(rowIndex, 5).Value = "C" Then
                Cells(rowIndex, 5).ClearContents
                Cells(rowIndex, 6).ClearContents
                Cells(rowIndex, 7).ClearContents

                With Cells(rowIndex, 2).Font
                    .Name = "Arial Narrow"
                    .Size = 8
                    .Italic = True
                    .ColorIndex = 16
                End With
                With Cells(rowIndex, 2)
                    .RowHeight = 12
                End With
            End If

            If Cells(rowIndex, 6) = "BN" Then
                Cells(rowIndex, 6).ClearContents
                If (Cells(rowIndex + 1, 5) <> "C") Then
                    Rows(rowIndex + 1).Insert
                    With Rows(rowIndex + 1)
                        .RowHeight = 3
                        .Font.Size = 2
                    End With
                    offset = offset + 1
                Else
                    Rows(rowIndex + 2).Insert
                    With Rows(rowIndex + 2)
                        .RowHeight = 3
                        .Font.Size = 2
                    End With
                    offset = offset + 2
                End If
            End If
        Else
            Cells(rowIndex, 2).WrapText = True
            Rows(rowIndex).AutoFit
        End If

        If Cells(rowIndex, 6).Value = "D" Then
            Cells(rowIndex, 6).ClearContents
            With Selection.Font
                .Underline = xlUnderlineStyleSingle
                .Bold = True
                .Italic = False
            End With
        End If

        If Cells(rowIndex, 6).Value = "E" Then
            With Selection.Font
                .Underline = xlUnderlineStyleSingle
                .Bold = False
            End With
        End If


        If Cells(rowIndex, 5).Value = "P" Then
            Cells(rowIndex, 5).ClearContens
        End If

        If ( _
            ((Cells(rowIndex, 5) = Cells(rowIndex - 1, 5)) Or (Cells(rowIndex, 5) = Cells(rowIndex - 2, 5))) And _
            ((Cells(rowIndex, 4) = "" And Cells(rowIndex - 1, 4) <> "") Or (InStr(Cells(rowIndex, 2).Text, ">"))) And _
            (Cells(rowIndex - 1, 2).Font.Size = 9) Or (Cells(rowIndex, 2).Font.Size = 9 And Cells(rowIndex - 1, 2).Font.Size = 9)) Then
            With Cells(rowIndex, 2).Font
                .Italic = True
                .ColorIndex = 16
                .Bold = False
                .Size = 8
                .Underline = False
            End With
            Cells(rowIndex, 2).WrapText = True
            Rows(rowIndex).AutoFit
        End If


        rowIndex = rowIndex + 1
    Loop
End With
End Sub

运行时错误出现在最后一个IF语句上

If ( _
            ((Cells(rowIndex, 5) = Cells(rowIndex - 1, 5)) Or (Cells(rowIndex, 5) = Cells(rowIndex - 2, 5))) And _
            ((Cells(rowIndex, 4) = "" And Cells(rowIndex - 1, 4) <> "") Or (InStr(Cells(rowIndex, 2).Text, ">"))) And _
            (Cells(rowIndex - 1, 2).Font.Size = 9) Or (Cells(rowIndex, 2).Font.Size = 9 And Cells(rowIndex - 1, 2).Font.Size = 9)) Then

0 个答案:

没有答案