Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA边框取决于页面大小_Vba_Excel - Fatal编程技术网

VBA边框取决于页面大小

VBA边框取决于页面大小,vba,excel,Vba,Excel,我希望根据页面大小(例如行数和列数)在每个excel页面周围创建边框。我试过这个,但它是特定的细胞 Sub AddBorders() With Range("B8:I10") .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlEdgeRight).LineStyle = xlContinuous .Borders(xlEdgeBottom).LineStyle = xlContinuous

我希望根据页面大小(例如行数和列数)在每个excel页面周围创建边框。我试过这个,但它是特定的细胞

 Sub AddBorders()

 With Range("B8:I10")
     .Borders(xlEdgeLeft).LineStyle = xlContinuous
     .Borders(xlEdgeRight).LineStyle = xlContinuous
     .Borders(xlEdgeBottom).LineStyle = xlContinuous
     .Borders(xlEdgeTop).LineStyle = xlContinuous End With End Sub
您可以使用:

ActiveSheet.UsedRange.BorderAround xlContinuous, xlMedium

我相信这样就可以了。

UsedRange
不应该用来查找最后一个有数据的单元格。这是非常不可靠的。您可能需要查看
usedrange
上的说明

始终查找最后一行和最后一列,然后创建范围。看看这个例子

我建议这样做

Sub AddBorders()
    Dim ws As Worksheet
    Dim lastrow As Long, lastcol As Long

    Set ws = Sheet1 '<~~ Change as applicable

    With ws
        '~~> Check if sheet has any data
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            '~~> Get Last Row
            lastrow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row

            '~~> Get Last Column
            lastcol = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByColumns, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Column

            '~~> Work with the range
            .Range(.Cells(1, 1), .Cells(lastrow, lastcol)).BorderAround _
            xlContinuous, xlMedium

        End If
    End With
End Sub
Sub-AddBorders()
将ws设置为工作表
调暗lastrow和lastcol一样长
设置ws=Sheet1'获取最后一行
lastrow=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
“~~>获取最后一列
lastcol=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
SearchOrder:=xlByColumns_
搜索方向:=xlPrevious_
MatchCase:=False)。列
“~~>使用范围
.Range(.Cells(1,1),.Cells(lastrow,lastcol)).borderabout_
连续的,中等的
如果结束
以
端接头
评论的后续行动


这样效果更好。唯一的问题是border并没有出现在任何图形/图表上。有没有办法做到这一点?感谢您的帮助–7分钟前用户1296762


另外,很抱歉,我们可以使用最后一行的底部边框+1,因为一些行被分组,因此如果不展开,就看不到行–5分钟前的user1296762

这就是你想要的吗

Sub AddBorders()
    Dim ws As Worksheet
    Dim lastrow As Long, lastcol As Long
    Dim shp As Shape

    Set ws = Sheet1 '<~~ Change as applicable

    With ws
        '~~> Check if sheet has any data
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            '~~> Get Last Row
            lastrow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row

            '~~> Get Last Column
            lastcol = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByColumns, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Column


        End If

        '~~> Loop through shapes and find the last row and column
        For Each shp In .Shapes
            If shp.BottomRightCell.Row > lastrow Then lastrow = shp.BottomRightCell.Row
            If shp.BottomRightCell.Column > lastcol Then lastcol = shp.BottomRightCell.Column
        Next

        If lastrow <> 0 And lastcol <> 0 Then
            'Also sorry can we have the bottom border last row+1 as some rows are
            'grouped up and therefore line can't be seen if not expanded
            '–  user1296762 2 mins ago
            lastrow = lastrow + 1: lastcol = lastcol + 1

            '~~> Work with the range
            .Range(.Cells(1, 1), .Cells(lastrow, lastcol)).BorderAround _
            xlContinuous, xlMedium
        End If
    End With
End Sub
Sub-AddBorders()
将ws设置为工作表
调暗lastrow和lastcol一样长
将shp变暗为形状
设置ws=Sheet1'获取最后一行
lastrow=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
“~~>获取最后一列
lastcol=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
SearchOrder:=xlByColumns_
搜索方向:=xlPrevious_
MatchCase:=False)。列
如果结束
“~~>在形状之间循环并找到最后一行和最后一列
对于形状中的每个shp
如果shp.BottomRightCell.Row>lastrow,则lastrow=shp.BottomRightCell.Row
如果shp.BottomRightCell.Column>lastcol,则lastcol=shp.BottomRightCell.Column
下一个
如果lastrow 0和lastcol 0,则

'也很抱歉,我们可以使用最后一行的底部边框+1,因为有些行是 '已分组,因此如果未展开,则无法看到该行 “–用户1296762 2分钟前 lastrow=lastrow+1:lastcol=lastcol+1 “~~>使用范围 .Range(.Cells(1,1),.Cells(lastrow,lastcol)).borderabout_ 连续的,中等的 如果结束 以 端接头
Sceenshot


这样效果更好。唯一的问题是border并没有出现在任何图形/图表上。有没有办法做到这一点?谢谢你的帮助,也很抱歉,我们可以有底部边框最后一行+1,因为一些行被分组,因此如果不展开,就看不到行更新帖子。您可能需要刷新页面