2013-08-18 3 views
0

셀 내용이 비어 있지 않으면 테두리가 필요한지 확인할 수있는 매크로를 만들어야합니다.매크로 빈 셀을 확인하고 그들을 강조 표시하십시오.

나는이 매크로 시도 :

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 

답변

0

을 시도해보십시오 :

Sub testborder() 

    Dim rRng As Range, row As Range, c As Range 
    Set rRng = Sheet1.Range("B2:D5") 
    'Clear existing 
    rRng.Borders.LineStyle = xlNone 

    For Each row In rRng.Rows 
     For Each c In row.Columns 
     'Apply new borders 
     If (c.Value > "") Then c.BorderAround xlContinuous 
     Next c 
    Next row 

End Sub 

또는, 더 간단한 루프 :

For Each c In rRng.Cells 
     'Apply new borders 
     If (c.Value > "") Then c.BorderAround xlContinuous 
    Next c 
+0

xlNone 열거 형이 작동하지만 잘못된 열거 형입니다. 여기에 참조 된 xlLineStyleNone이어야합니다. http://msdn.microsoft.com/en-us/library/office/ff821622.aspx – RobM

+0

또한 왜 행을 반복할까요? 그것은 덜 효율적입니다. 아, 편집을 보았습니다. 아마도 내 대답에서 빌린 것입니까? ;) – RobM

0

당신은 당신이 원하는대로 테스트 할 수 . 이 예제에서는 각 셀에 텍스트가 있는지 확인하여 테두리가있는 경우 각 셀에 테두리가 있는지 확인합니다.

Sub BorderForNonEmpty() 

    Dim myRange As Range 
    Set myRange = Sheet1.Range("B2:D5") 

    ' Clear existing borders 
    myRange.Borders.LineStyle = xlLineStyleNone 


    ' Test each cell and put a border around it if it has content 
    For Each myCell In myRange 
     If myCell.Text <> "" Then 
      myCell.BorderAround (xlContinuous) 
     End If 
    Next 

End Sub 
관련 문제