• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:エクセルでデータがある部分だけ範囲内の空白も込みで罫線で囲いたいです。)

エクセルでデータがある部分だけ範囲内の空白も込みで罫線で囲いたいです。

このQ&Aのポイント
  • エクセルでデータがある部分だけ、範囲内の空白も込みで罫線で囲みたいです。
  • エクセルのファイルを開いて、データのある部分だけを罫線で囲みたいです。空白の行や列も含めて対応したいです。
  • エクセルのデータの範囲を自動で判別して、空白も含めて罫線で囲む方法を教えてください。

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

  • ベストアンサー
  • myRange
  • ベストアンサー率71% (339/472)
回答No.4

  ----------------------------------  With ActiveSheet.UsedRange.Borders   .LineStyle = xlContinuous   .Weight = xlThin   .ColorIndex = xlAutomatic End With ---------------------------------- 実際は、行、列の最終を調べてやるべきです。  

gx9wx
質問者

お礼

ありがとうございます。 完璧に動作しました。 記述も短くて最高です。

gx9wx
質問者

補足

>実際は、行、列の最終を調べてやるべきです。 コマンドボタンを押すと、別ファイルのエクセルを開き いろいろな編集を行い、最後に罫線を引いて ファイル名をシステム日付で付けて登録マクロを引きつかず 別ファイルで保存までする という プロシージャーの中に組み込ませたいのです。 よって開いて編集した最終を調べる事ができないのです。 申し訳ありません。

全文を見る
すると、全ての回答が全文表示されます。

その他の回答 (3)

  • okormazd
  • ベストアンサー率50% (1224/2412)
回答No.3

Sub test() Dim r, c As Long r = ActiveSheet.UsedRange.Rows.Count c = ActiveSheet.UsedRange.Columns.Count With Range(Cells(1, 1), Cells(r, c)) .Borders(xlEdgeTop).LineStyle = xlContinuous .Borders(xlEdgeBottom).LineStyle = xlContinuous .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlEdgeRight).LineStyle = xlContinuous .Borders(xlInsideVertical).LineStyle = xlContinuous .Borders(xlInsideHorizontal).LineStyle = xlContinuous End With End Sub forでまわさないだけ速いでしょう。 すでにあるプロシージャとくつけるのなら, #1のお礼でエラーになるのは,Dim i,jで,iが前で宣言されているからでしょう。Dim jとすれば解消されるのではないですか。 これも,同様r,cが前に宣言されていれば,エラーになるので,Dimからはずす。

gx9wx
質問者

お礼

>すでにあるプロシージャとくつけるのなら, >#1のお礼でエラーになるのは,Dim i,jで,iが前で宣言されているからでしょう。Dim jとすれば解消>されるのではないですか。 >これも,同様r,cが前に宣言されていれば,エラーになるので,Dimからはずす。 はい。iが前で宣言されていました。 この教えていただいたのはr,cが前に宣言されておらず エラーにならずに実行できました。 あと、すごく速かったです。 5,000行ありますが、すでにあるプロシージャーの動作終了後 1秒以内で罫線引き終わりました。 ありがようございました。

全文を見る
すると、全ての回答が全文表示されます。
  • tom04
  • ベストアンサー率49% (2537/5117)
回答No.2

再びお邪魔します。 補足にある問題点について、こちらでデータを削除、行挿入・列挿入・行削除・列削除の後 前回のコードのマクロを実行してみましたが、問題なくA1セルから最終行・最終列まで罫線が表示されました。 というわけで補足にある原因についてはちょっと判りかねます。(ごめんなさい) それからエラーに関してですが、 (同じ適用範囲内で宣言が重複しています。)というエラーが出たのであればその通りに二重に宣言しているのだと思います。 コードをよくよく見返してみると同じ宣言をしている行がありませんか? そして、最後にコードの長さについてですが、ちゃんと正常に作動するのであれば問題ないと思いますよ。 コードの記述は人それぞれですので、「これでないといけない!」ということはありません。 考え方・方法によってコードの記述は十人十色ですので、あまり気にしなくて良いと思います。 (コードの記述をみると個性はあるみたいですが・・・それはそれで良いのでは? 私個人的には FOR~NEXTステートメントはよく使いたがる方です。) 重要なのは思った通りに動いてくれるかどうか?ということだと思います。 この程度ですが ごめんなさいね。m(__)m

gx9wx
質問者

お礼

連結するプロシージャー内の一部に以下の記述がありました。 'A列とB列の数値データーを名称に変更 Dim i As Long Dim Ar1 As Variant, Ar2 As Variant Dim LastRow As Long Ar1 = Array("", "入庫", "出庫", "返品") Ar2 = Array("", "社内製品", "社外製品", "受入検査") Application.ScreenUpdating = False LastRow = Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To LastRow If IsNumeric(Cells(i, 1).Value) Then If Cells(i, 1).Value > 0 And Cells(i, 1).Value < 4 Then Cells(i, 1).Value = Ar1(Cells(i, 1).Value) End If End If Next 'A列の値とB列の値から11,12,13列目に履歴表を作成 For i = 1 To LastRow If IsNumeric(Cells(i, 2).Value) Then If Cells(i, 2).Value > 0 And Cells(i, 2).Value < 3 Then Cells(i, 2).Value = Ar2(Cells(i, 2).Value) ElseIf Cells(i, 2).Value = 3 Then Cells(i, 2).Value = Ar2(Cells(i, 2).Value) Cells(i, 1).Value = Cells(i, 2).Value End If End If If Cells(i, 11).Value <> "" Then If Cells(i, 1).Value Like "入庫" Or Cells(i, 1).Value Like "返品" Then Cells(i, 12).Value = Cells(i, 11).Value: Cells(i, 11).ClearContents ElseIf Cells(i, 1).Value Like "出庫" Then Cells(i, 13).Value = Cells(i, 11).Value: Cells(i, 11).ClearContents End If End If Next Application.ScreenUpdating = False

gx9wx
質問者

補足

補足にある問題点について すいません。誤解を与えました。 これは教えてもらった内容を行う前に、独自で行った記述を 再チャレンジとして書きました。 でそれはそれで思ったように動作したのですが 根拠がなかったので、補足に書いてみました。 申し訳有りません。 教えていただいた内容では >こちらでデータを削除、行挿入・列挿入・行削除・列削除の後 >前回のコードのマクロを実行してみましたが、 >問題なくA1セルから最終行・最終列まで罫線が表示されました。 そのとうりで、ぜんぜん問題ありません。 ありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。
  • tom04
  • ベストアンサー率49% (2537/5117)
回答No.1

こんばんは! こんな感じでも良いのですかね? 色々方法はあるかと思いますが、一例です。 A1セルは必ず何かが入力されているものとします。 Sub test() Dim i, j As Long For i = 1 To ActiveSheet.UsedRange.Rows.Count For j = 1 To ActiveSheet.UsedRange.Columns.Count With Cells(i, j) .Borders(xlEdgeTop).LineStyle = xlContinuous .Borders(xlEdgeBottom).LineStyle = xlContinuous .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlEdgeRight).LineStyle = xlContinuous End With Next j Next i End Sub 参考になればよいのですが 的外れならごめんなさいね。m(__)m

gx9wx
質問者

お礼

ありがとうございます。単独なら動きました。 ですがすでに作成済みの他の編集マクロの記述の後に貼り付けてマクロを起動したら Dim i, j As Long のiに黄色が付いてコンパイルエラー (同じ適用範囲内で宣言が重複しています。) となります。 原因がわかりません。 作成済みのマクロ単独ではOK。 作成済みマクロ起動前に教えていただいたマクロを起動ではOK。 作成済みのマクロ起動で編集終了後に 教えていただいたマクロを起動でもOK。 ですが作成済みのマクロとこの教えていただいた物を 連結させると駄目です。 ですが作成済みのマクロ文の最後に Callで教えていただいたマクロを呼び出すと正常動作です。 なぜでしょうか?

gx9wx
質問者

補足

再チャレンジです。 Sub Macro3() Range("A1").Select Range(Selection, ActiveCell.SpecialCells(xlLastCell)).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 これなら全シートは選択されません。 でも本当にこれでいいのか不安です。 記述も長いし。 これはマクロの記録で セルA1を選択して、Ctrl+End を押して、罫線で格子を選んで終了です。 A1からM100までデータがあって 10列目に行挿入をして空白にして C列に列挿入してN列まで増やして A1からN101までに増えて、 セルN101をDELETEしましたが A1からN101まで囲まれました。 DELETEや行挿入や列挿入したら 目には見えないけどデータがあると判断したのでしょうか。 Ctrl+Aだと空白がある所のA1からC9までしか選択しませんので いいのでしょうか?

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

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

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

  • エクセルVBAで罫線挿入

    マクロの記録で下記のように記録されたものを簡潔にまとめるにはどのように記述したらいいでしょうか? Range("C3:F3").Select 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 .HorizontalAlignment = xlCenterAcrossSelection .VerticalAlignment = xlBottom End With

  • エクセルVBA 行列の数を指定して罫線を引くマクロ教えてください

    下のようにSheet1に罫線を引く「開始セル(左右端)」「行」「列」が書いてあります。 この条件で罫線をSheet2に引くようにしたいのですが、変数の設定の仕方などが分かっていないようで、できません。教えていただけないでしょうか。マクロの記録をとったところ、下のようになりました。よろしくお願いします。 開始セル Sheet2!A1・・・Sheet1のB1セル 行 8・・・Sheet1のB2セル 列 2・・・Sheet1のB3セル Sub borders() Range("A1:B8").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で罫線を引くマクロを書きたい

    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

  • ,最終行からA1の間に罫線を引く方法は?

    お願いします。 罫線を引くマクロを書きました。(下記) 範囲のJ26は最終行です。この表のデータ量は変化します。 最終行がJ26とは限りません。Z5000かもしれません。 その範囲に罫線をひくのですが、マクロ的に最終行を認知してA1 まで罫線を引くマクロをどう記述すればよいのか教えてください。 Sheets("読み込み").Select Range("A1:J26").Select Range("J26").Activate 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 Range("J3").Select End Sub

  • Excel2003枠を作るマクロ

    右側に空白の行を一つつくって枠を作りたいです。 たとえば、G100が一番右下とするとH100まで枠を作りたいのですが、きれいにかくにはどうしたらよいでしょうか? マクロ記録でやると、下のようになるのですが右下が100で有るとは限らないのでその行を定義する必要があると思うのですが、そのあたりがさっぱりわかりません。 よろしくお願いいたします。 Sub Macro1() Selection.End(xlDown).Select Range("H100").Select Range(Selection, Selection.End(xlUp)).Select Range(Selection, Selection.End(xlToLeft)).Select Range(Selection, Selection.End(xlToLeft)).Select Range(Selection, Selection.End(xlToLeft)).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

  • 選択範囲に黒の格子を付けるマクロ

    マクロ記録をつかって、選択している範囲に黒の格子を付けるマクロをボタンを作成しました。 (質問の下に、記録した内容を添付します) 実際に、格子を作るのには不要な行がたくさんあると思うのですが、不要な箇所をどう見つければいいのか教えてください。 個人的には、with以下の構文は標準設定しているだけなので不要かと思うのですが、何度も設定されていてくどく感じられます。 記録した後に、一括してこれらの記録を消す方法があればあわせて教えてください。 --- Sub 格子マクロ() 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

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

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (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 選択範囲の中で、更に一番上の行を指定したい!

    タイトルの通りです。 選択範囲に罫線を引くマクロを作成しました。 外に太い枠線、中は点線を引き、選択範囲の一番上の行を灰色の塗りつぶしにしたいのです。 しかし、罫線はうまくいきましたが、一番上の行の指定がうまくできません。 Sub 罫線をひく() ' With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With ActiveWindow.SmallScroll Down:=12  ←ここを変えたいのですが、どうすればよいかわかりません。 With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.499984740745262 .PatternTintAndShade = 0 End With End Sub どうすればよいか、もしご存知の方がいらっしゃいましたら、ご指導ください。 よろしくお願いたします。

  • マクロでマスを作成

    入力された数のマスを作成したいのですが、うまくマクロが作動しません。 (1)A1には作成したいマスの数が入力される (2)B1~B20に(1)で入力された数の□(しかく)で囲まれたマスが出来る  ※最高20列 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 (3)(2)では最高20マスしか出来ないので、60個以上のマスの場合  C1~C20、D1~D20に作成される よろしくお願いします

このQ&Aのポイント
  • AIRA COMPACT J-6の外部MIDI接続で音が出ない問題について質問があります。
  • Keylab essentialとのTRS/MIDI接続を確認し、MIDI設定も正常であるにもかかわらず音が出ない状況です。
  • 購入したType Aのケーブル以外に原因がある可能性があるか、アドバイスをいただけると助かります。
回答を見る

専門家に質問してみよう