Excel VBAで罫線を引くマクロを書きたいと思っています。
で、文末のコードを書きました。(というかマクロ記録したものほぼそのもの)
これだとある程度動くのですが、内側線が無いような範囲を選択した場合にはエラーになってしまいます。
内側の線を引く際にIF文をかまさなければならないように思うのですが、イマイチわかりません。
この点について教えてください。
また、コードが冗長であるようにも思えます。もう少しスマートな書き方があればあわせて教えてください。
よろしくお願いします。
Sub 枠線基本()
' 周囲
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
' 内側
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
End Sub
No.1
- 回答日時:
単純にエラートラップで回避してみては。
Sub 枠線基本()
' 周囲
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
On Error Resume Next
' 内側
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
On Error GoTo 0
End Sub
>単純にエラートラップで回避してみては。
最初はそうしようかとも思ったのですが、エラーを出すことが前提のコードって、あまりかっこよくない気がしてやめました。
いつか参考にしてみます。ありがとうございました。
No.2ベストアンサー
- 回答日時:
選択範囲の行数が2以上なら内側の水平線を、列数が2以上なら垂直線を引くようにしてあげれば良いのでは?
と思ったら、複数エリアを選択している時にちょいと面倒が・・・
(そんな場合を、想定しなくてもいいのかも知れませんが)
ということで、こんなのでどうでしょうか?
Sub test()
Dim rng As Range
Dim i As Integer
For Each rng In Selection.Areas
'//全部の罫線(内・外とも)
rng.Borders.LineStyle = xlContinuous
rng.Borders.Weight = xlThin
rng.Borders.ColorIndex = xlAutomatic
'//内側の罫線
For i = 11 To 12
If (i = 11 And rng.Columns.Count > 1) Or (i = 12 And rng.Rows.Count > 1) Then
rng.Borders(i).LineStyle = xlContinuous
rng.Borders(i).Weight = xlHairline
rng.Borders(i).ColorIndex = xlAutomatic
End If
Next i
Next rng
End Sub
複数エリアを想定しなければ、外側のループは不要です。
教えていただいたコードをベースに以下のようにアレンジしてみました。
大変助かりました。ありがとうございました。
Sub 枠線基本()
Dim rng As Range
For Each rng In Selection.Areas
' 全部の罫線(内・外とも)
rng.Borders.LineStyle = xlContinuous
rng.Borders.Weight = xlThin
rng.Borders.ColorIndex = xlAutomatic
' 内側の罫線
If (rng.Columns.Count > 1) Then
rng.Borders(xlInsideVertical).Weight = xlHairline
End If
If (rng.Rows.Count > 1) Then
rng.Borders(xlInsideHorizontal).Weight = xlHairline
End If
Next rng
End Sub
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Visual Basic(VBA) オートフィルタで抽出結果に 罫線をひく方法 1 2022/07/13 13:08
- Visual Basic(VBA) VBA 罫線について B列3行目から21行毎にデータがはいります。 データがはいったらデータが入った 6 2022/11/15 17:22
- Visual Basic(VBA) エクセルVBA ダブルクリックしたら色反転を指定したセルのみにしたい 2 2022/04/06 12:52
- Visual Basic(VBA) 複数セルに〇印をつけるマクロ 4 2022/09/07 05:33
- Excel(エクセル) 日付で矢印マクロ 4 2023/07/25 16:47
- Visual Basic(VBA) ExcelのVBAコードについて教えてください。 1 2023/08/10 14:24
- Visual Basic(VBA) VBA シート上にドロップダウンリストを作り、予め指定値をセットしたいのですが 1 2023/03/25 15:15
- Visual Basic(VBA) 【VBA】写真の貼り付けコードがうまく機能しません。 5 2022/09/01 18:43
- Visual Basic(VBA) マクロで最終行を取得してコピーしたい 3 2022/04/06 19:07
- Visual Basic(VBA) Powerpointでランダムな数字の結果を表示するマクロ 2 2023/08/04 10:04
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
エクセル、ページをまたがった...
-
ワードの文章囲み枠(?)を消...
-
ワードで勝手に点線ラインがでる
-
wordで罫線が引けない・・・
-
パワーポイントの表
-
エクセルの表をワードに貼り付...
-
PowerPointの表の罫線を二重罫...
-
PDFファイルの罫線を消す方法
-
エクセル 罫線の色が変えられ...
-
Excelの表をWordにリンク...
-
エクセルで遥か下まで線が出来る
-
Wordで作った「表の罫線を部分...
-
wordで作った表の罫線が消せな...
-
回覧板
-
Wordのページの上下に罫線みた...
-
パソコンのワードでこの様な割...
-
Wordで左右に分割
-
ワードで文字のところに長方形...
-
エクセル 2010 ピボットテーブ...
-
(Word)点線枠の消し方を教えて...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
エクセル、ページをまたがった...
-
ワードの文章囲み枠(?)を消...
-
wordで罫線が引けない・・・
-
ワードで勝手に点線ラインがでる
-
パワーポイントの表
-
PDFファイルの罫線を消す方法
-
PowerPointの表の罫線を二重罫...
-
ワードの表の罫線が見えるけど...
-
エクセル 罫線の色が変えられ...
-
Excelの表をWordにリンク...
-
回覧板
-
2段組みで左右別々に書き込め...
-
エクセルの表をワードに貼り付...
-
罫線を引いて保存しても消えて...
-
(Word)点線枠の消し方を教えて...
-
WORD- - - - -点線が消えません
-
WORD文書の青い枠線を消したい
-
オプションボタンの背景を透明...
-
Wordで左右に分割
-
エクセル 2010 ピボットテーブ...
おすすめ情報