본문 바로가기
Range object

[Range 개체] Borders속성,BorderAround 메서드 - 테두리 서식 설정하기

2023. 7. 2.

Range개체의 Borders속성을 이용하면 셀의 테두리를 설정할 수 있습니다.

아래 링크는 도움말입니다.

Range.Borders property (Excel)

Office VBA reference topic

learn.microsoft.com

Returns a Borders collection that represents the borders of a style or a range of cells (including a range defined as part of a conditional format).

번역하면 다음과 같습니다.

스타일 또는 셀 범위(조건부 서식의 일부로 정의된 범위를 포함)의 테두리를 나타내는 Borders 컬렉션을 반환합니다.

아래 그림은 Borders개체의 속성입니다.

Borders속성은 XlBordersIndex 열거형 값을 인수로 사용합니다.

XlBordersIndex에서 사용하는 내장 상수는 다음과 같습니다.

Name Value Description
xlDiagonalDown 5 범위에 있는 셀의 왼쪽 모서리에서 오른쪽 아래까지 이어지는 테두리입니다.
xlDiagonalUp 6 범위에 있는 셀의 왼쪽 아래 모서리에서 오른쪽 모서리까지 이어지는 테두리입니다.
xlEdgeBottom 9 범위 하단의 테두리입니다.
xlEdgeLeft 7 범위의 왼쪽 가장자리에 있는 테두리입니다.
xlEdgeRight 10 범위의 오른쪽 가장자리에 있는 테두리입니다.
xlEdgeTop 8 범위 상단의 테두리입니다.
xlInsideHorizontal 12 범위 외부의 테두리를 제외한 범위의 모든 셀에 대한 가로 테두리.
xlInsideVertical 11 범위 외부의 테두리를 제외한 범위의 모든 셀에 대한 세로 테두리.

B2:D2를 범위로 잡고 하단 테두리 설정하는 매크로 코드입니다.

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B2:D2").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End Sub​

위 코드에서 불필요한 부분을 제거한 코드입니다.

Sub Macro1()
    Range("B2:D2").Borders.LineStyle = xlNone ' 모든 테두리 제거
    Range("B2:D2").Borders(xlEdgeBottom).LineStyle = xlContinuous ' 하단 테두리 설정
End Sub

with문을 사용하면 다음과 같습니다.

Sub Macro1()

    With Range("B2:D2").Borders
        .LineStyle = xlNone    ' 모든 테두리 제거
        .Item(xlEdgeBottom).LineStyle = xlContinuous    ' 하단 테두리 설정
    End With
    
End Sub

Index를 생략하면 전체 테두리를 설정합니다.

Sub SetRangeBorders()

    With Range("B2:D4").Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    
End Sub

LineStyle속성은 테두리의 선 스타일을 반환하거나 설정합니다.

셀 서식 대화상자나 메뉴를 생각하시면 이해가 쉽습니다.

XlLineStyle 열거형 값은 다음과 같습니다.(도움말에는 내장 상수 값이 잘못 표기되어 있습니다)

Name Value Description
xlContinuous 1 Continuous line.
xlDash -4115 Dashed line.
xlDashDot 4 Alternating dashes and dots.
xlDashDotDot 5 Dash followed by two dots.
xlDot -4118 Dotted line.
xlDouble -4119 Double line.
xlLineStyleNone -4142 No line.
xlSlantDashDot 13 Slanted dashes.

Weight 속성은 테두리의 두께를 나타내는 XlBorderWeight 값을 반환하거나 설정합니다.

XlBorderWeight 열거형 값은 다음과 같습니다.

Name Value Description
xlHairline 1 Hairline (thinnest border).
xlMedium -4138 Medium.
xlThick 4 Thick (widest border).
xlThin 2 Thin.

Borders개체의 Value속성은 LineStyle속성과 같습니다.(테스트결과 1부터 13까지 사용 가능합니다)

Name LineStyle Value Description
xlContinuous 1 1,7 Continuous line.
xlDash -4115 2,8 Dashed line.
xlDashDot 4 4,10 Alternating dashes and dots.
xlDashDotDot 5 5,11 Dash followed by two dots.
xlDot -4118 3 Dotted line.
xlDouble -4119 9,12 Double line.
xlLineStyleNone -4142 No line.
xlSlantDashDot 13 6,13 Slanted dashes.

Borders개체의 ThemeColor,TintAndShade는 Interior의 Themecolor,TintAndShade와 같습니다.

Interior개체의 경우 매크로 기록을 하면 .ThemeColor = xlThemeColorAccent6라고 기록됩니다.

Borders개체는 .ThemeColor = 10라고 기록됩니다.

아래 그림에서 보듯이 같은 값입니다.

BorderAround 메서드는 외곽 테두리만 설정합니다.

문법은 다음과 같습니다.

expression.BorderAround (LineStyle, Weight, ColorIndex, Color, ThemeColor)

LineStyle,Weight 중 하나만 설정 가능합니다.테스트 한 결과 같이 사용하는 경우 LineStyle만 적용됩니다.

LineStyle의 기본값은 xlContinuous,Weight 의 기본값은 xlThin입니다.

아래 코드는 B2:D4의 테두리를 XlDot로 설정합니다.

Range("B2:D4").BorderAround LineStyle:=xlDot

'Range object' 카테고리의 다른 글

[Range 개체] Find메서드  (0) 2023.07.04
[Range 개체] Clear 관련 메서드  (0) 2023.07.03
[Range 개체] Interior속성  (0) 2023.07.01
[Range개체] Font속성  (0) 2023.07.01
[Range개체] Resize 속성  (0) 2023.06.29