エクセルの罫線を自動で引く方法(罫線編)

このQ&Aのポイント
  • 表の終端まで、データを入れた時点で自動的に罫線を引きたい。列方向の項目数は固定で、行方向が可変です。
  • 行方向のデータをカウントして、その数値を基に罫線を自動的に引きます。ただし、セルA1に入力したデータが終端と認識されます。
  • セルA1に入力したデータを取得し、数値に変換しています。その数値を行数に利用し、範囲内のセルに罫線を引きます。数値が255を超える場合や0以下の場合にはエラーメッセージを表示します。
回答を見る
  • ベストアンサー

エクセルでは自動は無理だと思うのですが・・・(罫線編)

たびたびすみません。excel達人先生方、よろしくご教示お願いします。 (要件) 表の終端まで、データを入れた時点で自動的に罫線を引きたい。 (列方向の項目数は固定。行方向が可変) (自分なりの努力結果) 行方向のデータをカウントして、その数値を基に罫線を自動的に引くところは作れましたが、ここでギブアップ。 例えばA列になんらかのデータがはいったら、そこを終端と認識し、そこまでの表に罫線を自動的に引くことはできないでしょうか? 以下、努力結果。 Private Sub Worksheet_Change(ByVal Target As Range) Dim varValue As Variant 'セルA1の値の取得 If Target.Address <> "$A$1" Then Exit Sub 'A1以外のイベントは無視 varValue = Range("A1").Value Cells.Borders.LineStyle = xlLineStyleNone '罫線クリア '数値変換 If IsNumeric(varValue) = False Then Exit Sub '数値以外の時は処理を抜ける varValue = Int(CLng(varValue)) '強制的に整数に変換しています。 'エラーチェック If varValue > 255 Then MsgBox "数値が大きすぎます。", vbCritical Exit Sub ElseIf varValue = 0 Then Exit Sub ElseIf varValue < 0 Then MsgBox "数値が小さすぎます。", vbCritical Exit Sub End If '罫線処理 With Range(Cells(1, 2), Cells(varValue + 1, 21)).Borders .LineStyle = xlContinuous .Weight = xlThin End With End Sub

質問者が選んだベストアンサー

  • ベストアンサー
  • n-jun
  • ベストアンサー率33% (959/2873)
回答No.1

With Range(Cells(1, 2), Cells(varValue + 1, 21)).Borders    ↓ With Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp).Resize(, 20)).Borders

その他の回答 (2)

  • n-jun
  • ベストアンサー率33% (959/2873)
回答No.3

ANo.1です。 A列という条件を見落としてましたのでスル~して下さい。

回答No.2

A1に条件書式で「数式が」=count($A1:$A$65536) で罫線で囲み、列 全体にペーストする。

関連するQ&A

  • エクセルのVBAについて教えてください!

    VBAの初心者です。 罫線に関するマクロで、以下のようなマクロを作りました。 このマクロを改造して、外枠全体に関して同じことができるように したくていろいろ試してみたのですが、どうしてもできません。 非常に初歩的な質問で申し訳ありませんが、よろしくお願い致します。 Sub 罫線右() With Selection If .Borders(xlEdgeRight).LineStyle = xlNone Then .Borders(xlEdgeRight).LineStyle = xlContinuous Exit Sub End If If .Borders(xlEdgeRight).LineStyle = xlContinuous And .Borders(xlEdgeRight).Weight = xlThin Then .Borders(xlEdgeRight).Weight = xlHairline Exit Sub Else .Borders(xlEdgeRight).LineStyle = xlNone End If End With End Sub

  • VBA罫線

    VBA罫線 a = 9 With Range(Cells(3, 2), Cells(a, 5)) .Borders(xlEdgeTop).LineStyle = xlContinuous .Borders(xlEdgeBottom).LineStyle = xlContinuous .Borders(xlEdgeRight).LineStyle = xlContinuous .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlInsideVertical).LineStyle = xlContinuous .Borders(xlInsideHorizontal).LineStyle = xlContinuous End With どこのワークシートかを指定する場合はどうしたらいいのでしょうか?

  • エクセル マクロ VBA 罫線 文字列

    職場で使う表をVBAマクロを用いて罫線作成をしています。 前任者のアレンジを頼まれたのですが前任者に連絡が取れず困っています。 表の特徴は以下のようになります。 ・A列を飛ばし、B列から2列飛びで文字を記入 ・b2=曜日、b3=1、b4=2、b5=3、b6=4、b7=空白のセットが曜日ごとに2セット×7日分 この表を ・b2=曜日、b3=-3、b4=-2、b5=-1、b6=0、b7=1、b8=2、b9=3、b10=4、b11=空白のセットが曜日ごとに2セット×7日分 に変更したいのですが空欄の場所がずれてしまい上手くいきません。 原本のマクロは以下です。 ---------------------------------------------------------------- Sub 罫線作成() Range(Cells(4, 1), Cells(86, 22)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone Selection.Borders(xlEdgeTop).LineStyle = xlNone Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ch1 = "月火水木金土日" For i = 4 To 76 Step 12 n1 = (i + 8) \ 12 Range(Cells(i, 1), Cells(i + 10, 22)).Select With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .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 'Cells(i, 1) = Mid(ch1, n1, 1) For i2 = 2 To 20 Step 3 For i3 = i To i + 10 nb1 = (i3 + 8) Mod 12 If nb1 = 0 Or nb1 = 6 Then Cells(i3, i2) = Mid(ch1, n1, 1) If nb1 = 1 Or nb1 = 7 Then Cells(i3, i2) = 1 If nb1 = 2 Or nb1 = 8 Then Cells(i3, i2) = 2 If nb1 = 3 Or nb1 = 9 Then Cells(i3, i2) = 3 If nb1 = 4 Or nb1 = 10 Then Cells(i3, i2) = 4 Next Next Next End Sub ---------------------------------------------------------------- 4行目から142行目まで使用することは分かっているのですが… どうかご助力お願いします。

  • すべてのシートでマクロを実行したい

    以下のプログラムでは、選択したシートのみマクロが動作しています。ネット検索で見よう見まねで作ったため何がまちがっているのかわかりません。ご教示いただけるとありがたいです。 ・月の予定表で利用者が休みの日に斜線を引くマクロ ・入力ミスを防ぐためシート保護をしている Sub すべてのシート() Dim s As Worksheet For Each s In Worksheets s.Select Call 斜線 Next End Sub Sub 斜線() ActiveSheet.Unprotect Password:="1234" For i = 1 To Range("E10").End(xlDown).Row Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlNone If Range("E10").Value = 0 Then Exit Sub If Cells(i, "E").Value = "日" And Range("BP9").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "月" And Range("BP10").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "火" And Range("BP11").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "水" And Range("BP12").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "木" And Range("BP13").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "金" And Range("BP14").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "土" And Range("BP15").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "AY").Value = "祝日" And Range("BP16").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If Next i ActiveSheet.Protect Password:="1234" End Sub

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

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

  • Excel VBAの罫線描画マクロをOpenOfficeCulcで実行。罫線が描画されない。

    Excel VBAで作成したマクロをOpenOffice.orgのCulcに単純移行して 実行してみたのですが、コードは実行されるのに罫線が描画されない 現象が発生しています。 罫線描画はCulcとExcelで互換が遅れている部分だと聞いていますが、 Culc独自のマクロで作成するしか回避方法はないのでしょうか? マクロは下記のような構成になっています。 REM ***** BASIC ***** Option VBASupport 1 Sub draw_line() With Worksheets("drawline") .Range("B2:E8").Borders(xlEdgeLeft).LineStyle = xlContinuous .Range("B2:E8").Borders(xlEdgeTop).LineStyle = xlContinuous .Range("B2:E8").Borders(xlEdgeBottom).LineStyle = xlContinuous .Range("B2:E8").Borders(xlEdgeRight).LineStyle = xlContinuous .Range("B2:E8").Borders(xlInsideVertical).LineStyle = xlContinuous .Range("B2:E8").Borders(xlInsideHorizontal).LineStyle = xlContinuous End With End Sub

  • 表に罫線を最終列まで引きたい

    windows7 とExcel2007でマクロ作成中の初心者です。 最初に、D2:E34を選んで罫線をひき、列を2列移動して、また罫線を引きます。 これをデータのある最終列まで繰り返したいのですが、うまくいきません。 どうしたらよろしいでしょうか。 Sub 勤怠に罫線() Dim n As Integer For n = 1 To 5 勤怠に罫線 Selection.Offset(0, 2).Select 勤怠に罫線 Next n End Sub 以下はマクロの自動記録でコードを書きました。 Sub 勤怠に罫線() Range("D2:E34").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("D2:E34").Select End Sub

  • 別シートに罫線がひけない

    表題どおりなのですが、別シートに罫線がかけません。 例えばSheet1にあるボタンをクリックするとSheet2に罫線をかく。 (コードは下記参照)としたときにエラーが発生します。 「1004 Rangeメソッドは失敗しました。」 そのため「ActiveSheet.」をはずしてみると”Sheet1”に描画されてしまいます。 なにか宣言が必要なのでしょうか? Private Sub CommandButton1_Click() Worksheets("sheet2").Activate '.Selectでも同じ For i = 4 To Range("G30").Column ActiveSheet.Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThin ActiveSheet.Range(Cells(4, i), Cells(30,i)).Borders(xlLeft).LineStyle = xlContinuous Next End Sub

  • ワークシートのセルの書式設定の罫線をマクロでひく。

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (1)のところでBORDERクラスのlinestyle プロパティを設定できません。がでる対策をおしえてください。 Sub Macro1() ' Dim d As Long Sheets("abc").Select '罫線を引く d = Range("A65536").End(xlUp).Row Range("A1", Cells(d, 1)).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  ‘(1) .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End Sub

  • VBA 罫線

    VBA 罫線 仮にb=15 cells(12,2)の下の線をCells(12, 3)~ Cells(2, b)まで同じ線を引く方法です。 Dim sh2 As Worksheet Set sh2 = Worksheets("test") a =12 今開いてるシートではなくて別のシートのを選択したいのですが エラーになってしまいます。 全てのcellsの前にsh2を入れればいけると思ったのですが駄目でした。 どーうしたらいいのでしょうか? Do Until (sh2.Cells(a, 1) = "合計") Or (sh2.Cells(a, 2) = "合計") With .Range(Cells(a, 3), Cells(2, b)).Borders(xlEdgeBottom) .LineStyle = Cells(a, 2).Borders(xlEdgeBottom).LineStyle .Weight = Cells(a, 2).Borders(xlEdgeBottom).Weight .ColorIndex = Cells(a, 2).Borders(xlEdgeBottom).ColorIndex End With a = a + 1 Loop

専門家に質問してみよう