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

には、例えば、これを試してみてください。私はC#でこれをやったので、構文が正しいことを

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

希望。

+0

実際には役に立ちませんが、これは範囲では動作しますが、セルでは動作しません。 – CustomX

+0

これは正確には何ですか:Range( "C11")。境界線(xlEdgeRight).LineStyle = xlContinuous またはこれより:Range( "A15:A15")境界線(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 
関連する問題