2012-10-29 118 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

你只需要一個單一的代碼行設置範圍內每個單元格周圍的邊框:

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 
1

對於添加邊框嘗試這個,例如:

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

希望這種語法是正確的,因爲我已經在C#中做到了這一點。

+0

沒有什麼幫助,這可以在一個範圍內工作,但不是每個單元格。 – CustomX

+0

,究竟是什麼:Range(「C11」)。Borders(xlEdgeRight).LineStyle = xlContinuous or this:Range(「A15:A15」)。Borders(xlEdgeBottom).LineStyle = xlContinuous – Sylca

+0

Idk這就是你'我在最初的代碼中沒有提供任何東西。 – CustomX

8

下可以用任何範圍內,參數被稱爲:

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 

例子:

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

這裏的另一種方式

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 
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 
相關問題