VBA如果判断单元格是否有边框?
- Private Sub CommandButton1_Click()
- Dim sta As String, enb As String
- 序号 = 1
- sta = Selection.Row
- col = Selection.Column
- enb = Selection.Rows.Count + sta - 1
- For i = sta To enb
- If Selection.Borders.LineStyle <> xlNone Then
- MsgBox "有边框"
- Else
- MsgBox "无边框"
- End If
- Next
- End Sub
此种方法仅能对上下左右都有边框的情况下才起作用。当单元格只有部分边框的情况下会判断不准
- Sub test()
- Dim i&, n As Boolean, rng As Range
- For Each rng In Selection
- For i = 5 To 12 '不判断单元格内斜线就从 7 to 12
- If rng.Borders(i).LineStyle <> xlNone Then n = True: Exit For
- Next i
- Next
- If n Then MsgBox "有" Else MsgBox "无"
- End Sub