Arrays 制作一个数组,在一次扫描中完成所有边界设置

Arrays 制作一个数组,在一次扫描中完成所有边界设置,arrays,excel,vba,Arrays,Excel,Vba,我正在尝试设置一个数组来提取excel中的边框类型,这样我就可以在几行中创建所有边框,而不是通常的>40行 很明显,我遗漏了一些明显的东西,但目前我无法解决 有人能帮忙吗 当前错误: 对象不支持此属性或方法(错误438) 这将拉到.LineStyle=xlContinuous行 i不是解决这个问题的正确方法吗?谢谢你的帮助 Dim wb As Workbook, ws As Worksheet, rng As Range, LastRow As Long, rng2 As Range, bord

我正在尝试设置一个数组来提取excel中的边框类型,这样我就可以在几行中创建所有边框,而不是通常的>40行

很明显,我遗漏了一些明显的东西,但目前我无法解决 有人能帮忙吗

当前错误:
对象不支持此属性或方法(错误438)

这将拉到
.LineStyle=xlContinuous

i
不是解决这个问题的正确方法吗?谢谢你的帮助

Dim wb As Workbook, ws As Worksheet, rng As Range, LastRow As Long, rng2 As Range, borderType, i

borderType = Array("xlEdgeLeft", "xlEdgeTop", "xlEdgeBottom", "xlEdgeRight", "xlInsideVertical", "xlInsideHorizontal")
Set wb = ThisWorkbook
Set ws = Worksheets("Data")
Set rng = ws.Cells(1, 1)

    LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row                  'Finds the bottom populated row

Set rng2 = ws.Range(rng, ws.Cells(LastRow, 15))

    With rng2
        .AutoFilter
        .EntireColumn.AutoFit
        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone


        For Each i In borderType
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        Next

    End With

像这样的东西可能就是你想要的:

Sub tgr()

    Dim ws As Worksheet
    Dim aBorderSettings() As Long
    Dim i As Long

    Set ws = ThisWorkbook.Worksheets("Data")

    ReDim aBorderSettings(1 To 8, 1 To 2)
        aBorderSettings(1, 1) = xlDiagonalDown:     aBorderSettings(1, 2) = xlNone
        aBorderSettings(2, 1) = xlDiagonalUp:       aBorderSettings(2, 2) = xlNone
        aBorderSettings(3, 1) = xlEdgeBottom:       aBorderSettings(3, 2) = xlContinuous
        aBorderSettings(4, 1) = xlEdgeLeft:         aBorderSettings(4, 2) = xlContinuous
        aBorderSettings(5, 1) = xlEdgeRight:        aBorderSettings(5, 2) = xlContinuous
        aBorderSettings(6, 1) = xlEdgeTop:          aBorderSettings(6, 2) = xlContinuous
        aBorderSettings(7, 1) = xlInsideHorizontal: aBorderSettings(7, 2) = xlContinuous
        aBorderSettings(8, 1) = xlInsideVertical:   aBorderSettings(8, 2) = xlContinuous

    With ws.Range("A1:O" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
        .AutoFilter
        .EntireColumn.AutoFit
        For i = LBound(aBorderSettings, 1) To UBound(aBorderSettings, 1)
            .Borders(aBorderSettings(i, 1)).LineStyle = aBorderSettings(i, 2)
            If aBorderSettings(i, 2) <> xlNone Then
                .Borders(aBorderSettings(i, 1)).ColorIndex = 0
                .Borders(aBorderSettings(i, 1)).TintAndShade = 0
                .Borders(aBorderSettings(i, 1)).Weight = xlThin
            End If
        Next i
    End With

End Sub
Sub-tgr()
将ws设置为工作表
Dim ABORDERSTINGS()的长度为
我想我会坚持多久
设置ws=ThisWorkbook.Worksheets(“数据”)
重拨中止设置(1到8,1到2)
aBorderSettings(1,1)=xlDiagonalDown:aBorderSettings(1,2)=xlNone
aBorderSettings(2,1)=xlDiagonalUp:aBorderSettings(2,2)=xlNone
AborterSettings(3,1)=xlEdgeBottom:AborterSettings(3,2)=xlContinuous
中止设置(4,1)=xlEdgeLeft:中止设置(4,2)=xlContinuous
aBorderSettings(5,1)=xledRight:aBorderSettings(5,2)=xlContinuous
aBorderSettings(6,1)=xlEdgeTop:aBorderSettings(6,2)=xlContinuous
AborterSettings(7,1)=xlInsideHorizontal:AborterSettings(7,2)=xlContinuous
aBorderSettings(8,1)=xlInsideVertical:aBorderSettings(8,2)=xlContinuous
使用ws.Range(“A1:O”和ws.Cells(ws.Rows.Count,“A”).End(xlUp.Row)
.自动过滤器
.全自动装配
对于i=LBound(aBorderSettings,1)到UBound(aBorderSettings,1)
.Borders(aBorderSettings(i,1)).LineStyle=aBorderSettings(i,2)
如果中止设置(i,2)xlNone,则
.Borders(aBorderSettings(i,1)).ColorIndex=0
.Borders(aBorderSettings(i,1)).TintAndShade=0
.Borders(aBorderSettings(i,1)).Weight=xlThin
如果结束
接下来我
以
端接头

borderetype是一个字符串数组-因此i是一个字符串而不是一个对象。但是在任何情况下,循环中的代码都引用WITHRNG2行,rng2没有linestyle属性。这是一个很好的答案。非常感谢。我没有使用过
LBound
UBound
。我甚至没有想到使用多个阵列同时提取我需要的所有内容。多谢各位