去评论
距米网-精简版

VBA如果判断单元格是否有边框?

JUMU
2020/02/11 19:33:25
  1. Private Sub CommandButton1_Click()
  2. Dim sta As String, enb As String
  3. 序号 = 1
  4. sta = Selection.Row
  5. col = Selection.Column
  6. enb = Selection.Rows.Count + sta - 1
  7. For i = sta To enb
  8.    If Selection.Borders.LineStyle <> xlNone Then
  9.       MsgBox "有边框"
  10.    Else
  11.       MsgBox "无边框"
  12.    End If
  13. Next
  14. End Sub

此种方法仅能对上下左右都有边框的情况下才起作用。当单元格只有部分边框的情况下会判断不准

  1. Sub test()
  2. Dim i&, n As Boolean, rng As Range
  3. For Each rng In Selection
  4.   For i = 5 To 12        '不判断单元格内斜线就从 7 to 12
  5.     If rng.Borders(i).LineStyle <> xlNone Then n = True: Exit For
  6.   Next i
  7. Next
  8. If n Then MsgBox "有" Else MsgBox "无"
  9. End Sub