Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
Excel 指定范围和图纸的边框_Excel_Vba - Fatal编程技术网

Excel 指定范围和图纸的边框

Excel 指定范围和图纸的边框,excel,vba,Excel,Vba,我使用下面的代码将边框应用于范围内的所有单元格 Sub Borders() Application.ScreenUpdating = False 'Prevents screen refreshing Dim lngLstCol As Long, lngLstRow As Long, ws As Worksheet Dim rngCell As Range, r As Long, c As Long Dim skp As Boolean For Each ws In ActiveWor

我使用下面的代码将边框应用于范围内的所有单元格

Sub Borders()

Application.ScreenUpdating = False    'Prevents screen refreshing
Dim lngLstCol As Long, lngLstRow As Long, ws As Worksheet
Dim rngCell As Range, r As Long, c As Long
Dim skp As Boolean

For Each ws In ActiveWorkbook.Worksheets
    lngLstRow = ws.UsedRange.Rows.Count
    lngLstCol = ws.UsedRange.Columns.Count

    For Each rngCell In ws.Range("A1:A" & lngLstRow)
        If rngCell.Value <> "" Then
            r = rngCell.Row
            c = rngCell.Column

            With ws.Range(ws.Cells(r, c), ws.Cells(r, lngLstCol)).Borders
                .LineStyle = xlContinuous    'Setting style of border line
                .Weight = xlThin    'Setting weight of border line
                .ColorIndex = xlAutomatic    'Setting colour of border line
            End With
        End If
    Next
Next

Application.ScreenUpdating = True    'Enables screen refreshing
End Sub
子边框()
Application.ScreenUpdate=False“阻止屏幕刷新
变暗lngLstCol为长,lngLstRow为长,ws为工作表
变暗RNG单元格作为范围,r作为长度,c作为长度
Dim-skp为布尔型
对于ActiveWorkbook.Worksheets中的每个ws
lngLstRow=ws.UsedRange.Rows.Count
lngLstCol=ws.UsedRange.Columns.Count
对于ws.范围内的每个rngCell(“A1:A”和lngLstRow)
如果rngCell.Value为“”,则
r=rngCell.Row
c=rngCell.Column
使用ws.Range(ws.Cells(r,c),ws.Cells(r,lngLstCol)).border
.LineStyle=xlContinuous“设置边框线的样式
.Weight=xlThin“边界线的设置权重”
.ColorIndex=xl自动设置边框线的颜色
以
如果结束
下一个
下一个
Application.ScreenUpdate=True“启用屏幕刷新
端接头

如何使用此代码忽略第一张图纸,并仅对之后的图纸应用边框

只需从2开始,按索引循环浏览表格即可

注意:用户可以重新订购图纸,因此“第一张图纸”可能不是您期望的图纸

此代码还说明了工作表以外的可能的工作表类型

Sub Demo()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim idx As Long

    Set wb = ActiveWorkbook
    For idx = 2 To wb.Sheets.Count
        If wb.Sheets(idx).Type = xlWorksheet Then
            Set ws = wb.Sheets(idx)
            With ws
                'all your ws code ...
            End With
        End If
    Next

End Sub
若要处理用户移动图纸的可能性,可以使用

在此代码中,
SheetX
是您不想格式化的工作表的代码名(根据需要进行调整)

子边框()
将ws作为工作表,行作为范围
对于工作表中的每个ws
如果ws.Name为“Sheet1”,则“将Sheet1替换为要跳过的工作表的名称”
对于ws.UsedRange.Rows中的每一行
如果行(1)”,则row.Borders.LineStyle=xlContinuous
下一个
如果结束
下一个
端接头

很抱歉,我无法将我的代码添加到您提供的解决方案中。我有错误。然后用你尝试过的和你得到的错误更新你的Q Hanks,Chris。很抱歉反应太晚。我能够调试并成功运行代码。
Sub Demo()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim idx As Long

    Set wb = ActiveWorkbook
    For idx = 1 To wb.Sheets.Count
        If wb.Sheets(idx).Type = xlWorksheet Then
            If Not wb.Sheets(idx) Is SheetX Then
                Set ws = wb.Sheets(idx)
                With ws
                    'all your ws code ...
                End With
            End If
        End If
    Next

End Sub
Sub Borders()
    Dim ws As Worksheet, row As Range

    For Each ws In Worksheets
        If ws.Name <> "Sheet1" Then      ' replace Sheet1 with the name of the Sheet to skip
            For Each row In ws.UsedRange.Rows
                If row(1) <> "" Then row.Borders.LineStyle = xlContinuous    
            Next
        End If
    Next
End Sub