2012-10-29 5 views
22

특정 범위의 모든 셀 주위에 테두리를 추가하는 간단한 함수를 만들려고합니다. 훌륭한 레코딩을 사용하면 많은 코드가 생성되어 꽤 쓸모가 없습니다. 아래 코드는 데이터의 '테이블'을 표시합니다.이 범위의 각 셀 주변에 테두리를 추가하고 싶습니다. 온라인 나는 이것에 대한 간단하거나 분명한 대답을 찾을 수 없었습니다.범위의 각 셀 주변 테두리

모든 도움을 많이 받으실 수 있습니다! 국경을 추가

Set DT = Sheets("DATA") 
endRow = DT.Range("F" & Rows.Count).End(xlUp).Row 
result = 3 

For I = 2 To endRow 
    If DT.Cells(I, 6).Value = Range("B1").Value Then 
     Range("A" & result) = DT.Cells(I, 6).Value 
     Range("B" & result) = DT.Cells(I, 1).Value 
     Range("C" & result) = DT.Cells(I, 24).Value 
     Range("D" & result) = DT.Cells(I, 37).Value 
     Range("E" & result) = DT.Cells(I, 3).Value 
     Range("F" & result) = DT.Cells(I, 15).Value 
     Range("G" & result) = DT.Cells(I, 12).Value 
     Range("H" & result) = DT.Cells(I, 40).Value 
     Range("I" & result) = DT.Cells(I, 23).Value 
     result = result + 1 
    End If 
Next I 
+1

이 사람들을 혼동 것처럼 내가 본 내 제목을 편집했다. – CustomX

답변

77

여기

Call SetRangeBorder(Range("C11")) 
Call SetRangeBorder(Range("A" & result)) 
Call SetRangeBorder(DT.Cells(I, 6)) 
Call SetRangeBorder(Range("A3:I" & endRow)) 
1

예를 들어,이 시도 : 나는 C#에서 이런 짓을했기 때문에 구문이 올바른지

Range("C11").Borders(xlEdgeRight).LineStyle = xlContinuous 
Range("A15:D15").Borders(xlEdgeBottom).LineStyle = xlContinuous 

희망을.

+0

정말 도움이되지는 않지만 범위에서 작동하지만 셀당 작동하지는 않습니다. – CustomX

+0

정확히 무엇입니까 범위 ("C11"). 테두리 (xlEdgeRight) .LineStyle = xlContinuous 또는이 : 범위 ("A15 : A15") 테두리 (xlEdgeBottom) .LineStyle = xlContinuous – Sylca

+0

Idk that ' 초기 코드에 제공된 내용이 없습니다. – CustomX

8
다음은 매개 변수로 어떤 범위로 호출 할 수 있습니다

: 당신은 단지에 단 한 줄의 코드를 필요로하는 또 다른 방법

Sub testborder() 

    Dim rRng As Range 

    Set rRng = Sheet1.Range("B2:D5") 

    'Clear existing 
    rRng.Borders.LineStyle = xlNone 

    'Apply new borders 
    rRng.BorderAround xlContinuous 
    rRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous 
    rRng.Borders(xlInsideVertical).LineStyle = xlContinuous 

End Sub 
5

입니다 :

Option Explicit 

Sub SetRangeBorder(poRng As Range) 
    If Not poRng Is Nothing Then 
     poRng.Borders(xlDiagonalDown).LineStyle = xlNone 
     poRng.Borders(xlDiagonalUp).LineStyle = xlNone 
     poRng.Borders(xlEdgeLeft).LineStyle = xlContinuous 
     poRng.Borders(xlEdgeTop).LineStyle = xlContinuous 
     poRng.Borders(xlEdgeBottom).LineStyle = xlContinuous 
     poRng.Borders(xlEdgeRight).LineStyle = xlContinuous 
     poRng.Borders(xlInsideVertical).LineStyle = xlContinuous 
     poRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous 
    End If 
End Sub 

예 범위 내의 모든 셀 주변에 테두리를 설정하십시오.

Range("A1:F20").Borders.LineStyle = xlContinuous

각 셀 주변의 테두리에 여러 효과를 적용하는 것도 쉽습니다. 예를 들어

:

Sub RedOutlineCells() 
    Dim rng As Range 

    Set rng = Range("A1:F20") 

    With rng.Borders 
     .LineStyle = xlContinuous 
     .Color = vbRed 
     .Weight = xlThin 
    End With 
End Sub 
2

내가 처음이 문제를 찾고, 그러나 여기에서 나의 최종 결과였다 때이 페이지를 찾지 못했습니다. 솔직히 수퍼 유저를 찾아 보냈지 만 거기에 속하는지 아니면 여기에 있는지 확실하지 않습니다.

샘플 전화

Call BoxIt(Range("A1:z25")) 

서브 루틴

Sub BoxIt(aRng As Range) 
On Error Resume Next 

    With aRng 

     'Clear existing 
     .Borders.LineStyle = xlNone 

     'Apply new borders 
     .BorderAround xlContinuous, xlThick, 0 
     With .Borders(xlInsideVertical) 
      .LineStyle = xlContinuous 
      .ColorIndex = 0 
      .Weight = xlMedium 
     End With 
     With .Borders(xlInsideHorizontal) 
      .LineStyle = xlContinuous 
      .ColorIndex = 0 
      .Weight = xlMedium 
     End With 
    End With 

End Sub 
0
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid 
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid 
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid 
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid 
관련 문제