Excel 2016 vba宏将边框设置为一个单元格区域,里面有过滤后的单元格会产生错误(使用Excel 2013一切都很好)

问题描述 投票:0回答:2

----更新了更多细节---

我已经制作了一个适用于Excel 2013的vba宏,但是Excel 2016有错误。宏很简单,取自“录制的宏”:它设置了一些单元格的边框。

问题(我想)是包含过滤行的单元格:

column_1 
cells(1;1) = "aa"
cells(2;1) = 2
cells(3;1) = 1
cells(4;1) = 2
cells(5;1) = 1
cells(6;1) = 1

在第一行用“1”过滤

enter image description here

因此,运行以下宏,“。weight”行上的错误“1004”

enter image description here

给你好,你有:enter image description here

如果你现在停止宏并尝试保存文件,你会收到一个错误:enter image description here

请注意,这只发生在Excel 2016,Excel 2013没有问题

这是完整的宏:Option Explicit

        Sub test()
            Sheets(1).Select
            Range("A1:A6").Select
            Selection.Borders(xlDiagonalDown).LineStyle = xlNone
            Selection.Borders(xlDiagonalUp).LineStyle = xlNone
            With Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin   ' ==>>>>ERROR HERE
            End With
            With Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With Selection.Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With

请帮助Thx

excel vba border cell
2个回答
0
投票

改变范围并尝试:

Option Explicit

Sub test()

    With ThisWorkbook.Worksheets("Sheet1").Range("A1:A6").Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With

End Sub

0
投票

我一直在阅读Microsoft文档,似乎选择是不必要的。试试这个:

sub test()

Range("A1:A6").Borders.LineStyle = xlNone

With Range("A1:A6").Borders(xlEdgeLeft)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin   ' ==>>ERROR HERE

End With

End sub

您可能必须在范围或工作表(“Sheet1”)之前抛出ActiveWorksheet,具体取决于宏运行时您使用的工作表。希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.