工作表上的宏错误更改

时间:2015-04-23 11:56:21

标签: excel vba excel-vba

我一直在用Excel工作。感谢Stack Overflow用户:),我有几个workheet_change宏。一切都运行完美,但其中一个在某些情况下会给出运行时错误“1004”。

  Application.ScreenUpdating = False
If Target.Count > 1 Then Exit Sub
If Target.Column <= 25 Then
    If Target.value <> "" Then
    Range("A" & Target.Row & ":Y" & Target.Row).Select
    Selection.borders(xlDiagonalDown).LineStyle = xlNone
    Selection.borders(xlDiagonalUp).LineStyle = xlNone

        With Selection.borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

    Selection.borders(xlInsideHorizontal).LineStyle = xlContinuous
    Application.ScreenUpdating = True
    End If
End If

因此,每当我在单元格中输入数据时,宏会自动从A列开始自动放置边框到Y列。它可以正常工作但是如果选择了一个单元格(不是单个选择而是双击选择进入单元格),如果我尝试要更改工作表,它会突出显示这条突出显示的行:

Range("A" & Target.Row & ":Y" & Target.Row).Select

我可以取消选择单元格并更改工作表,但我想知道它是否可以在宏中修复?

1 个答案:

答案 0 :(得分:0)

根本不需要选择:

If Target.Column <= 25 Then
    If Target.Value <> "" Then
        With Range("A" & Target.Row & ":Y" & Target.Row)
            .Borders(xlDiagonalDown).LineStyle = xlNone
            .Borders(xlDiagonalUp).LineStyle = xlNone

            With .Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With

            .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        End With
        Application.ScreenUpdating = True
    End If
End If