指定范围和工作表的边框

时间:2018-05-28 22:19:13

标签: excel excel-vba 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 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

如何使用此代码忽略第一张纸张并在此之后仅将纸张应用于纸张?

2 个答案:

答案 0 :(得分:1)

只需按索引循环播放,从2开始。

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

此代码还考虑了工作表

以外的可能的工作表类型
  function getOS() {
  var userAgent = window.navigator.userAgent,
      platform = window.navigator.platform,
      macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
      windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
      iosPlatforms = ['iPhone', 'iPad', 'iPod'],
      os = null;

  if (macosPlatforms.indexOf(platform) !== -1) {
    os = 'Mac OS';
  } else if (iosPlatforms.indexOf(platform) !== -1) {
    os = 'iOS';
  } else if (windowsPlatforms.indexOf(platform) !== -1) {
    os = 'Windows';
  } else if (/Android/.test(userAgent)) {
    os = 'Android';
  } else if (!os && /Linux/.test(platform)) {
    os = 'Linux';
  }

  return os;
}

alert(getOS());

要处理用户移动工作表的可能性,您可以使用工作表code name

在此代码中,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

答案 1 :(得分:0)

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