dポイントプレゼントキャンペーン実施中!

エクセルでデータがある部分だけ罫線で囲いたいです。

エクセルのファイルを開いて、データのある部分だけを罫線で囲みたいです。
データーは常に列数も行数も違います。
マクロの記録で行ったら、以下のようになりました。
もう少し短い文章ではできないでしょうか?

Sub Macro1()
'
' Macro1 Macro
' マクロ記録日 : 2010/9/22

'
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub

A 回答 (3件)

.Weight = xlThin


.ColorIndex = xlAutomatic
これらは省略できるので、次の一文だけでよいです。
Range("A1").CurrentRegion.Borders.LineStyle = xlContinuous
    • good
    • 7
この回答へのお礼

完璧でした。
ありがとうございます。

お礼日時:2010/09/22 17:37

CurrentReigonプロパティを使って一案。


   
'----------------------------------------
Sub test()
  With Range("A1").CurrentRegion.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
  End With
End Sub
'---------------------------------------
 
以上です。
    • good
    • 1
この回答へのお礼

完璧でした。

ありがとうございます。

お礼日時:2010/09/22 10:55

条件付書式設定を使えば簡単なのでは?



対象となるセルの範囲全体を選択して、
条件付きの書式設定
セルの値が  次の値に等しくない  =""
書式 → 罫線 → 外枠

ご所望のイメージとは違いますか?
    • good
    • 0
この回答へのお礼

ありがとうございました。

お礼日時:2010/09/22 10:54

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A