VBA初心者の高速化マクロ修正

このQ&Aのポイント
  • VBA初心者が作成した修正済みの高速化マクロ
  • セルのフォント色、背景色、罫線設定を一括で行う
  • 問題数に応じてセルの設定を変更し、印刷範囲を指定する
回答を見る
  • ベストアンサー

このマクロを高速化させるにはどうすればいいですか?

VBA初心者で、とりあえず頑張って作ってみました。以下のVBAでの修正点を教えてください。 (英単語の小テスト用につくりました。) Range("C3:E22,H3:J22").Select Selection.Font.ColorIndex = 2 Selection.Interior.ColorIndex = 2 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 Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone ' ここまでは、共通の動作 Range("A1").Select ActiveCell.FormulaR1C1 = "20" ' 問題数に応じて、数字を変更 Range("C3:E22,H3:J22").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.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With ' 罫線を引く Range("D3:E22,I3:J22").Select Selection.Font.ColorIndex = 1 ' 文字を黒くする Range("C3:C22,H3:H22").Select Selection.Interior.ColorIndex = 16 ' セルをグレーにする Rows("3:18").Select Selection.RowHeight = 31.5 ' セルの幅を指定 ActiveSheet.PageSetup.PrintArea = "$B$1:$K$25" ' 印刷範囲を指定 Range("U3:U42").Select Selection.ClearContents ' 四線を消去 Range("D3:D4").Select Calculate ' 再計算完了 宜しくお願いします。 また、このVBAは慣れている人から見ると何点くらいですか?(感覚で結構です)

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

  • ベストアンサー
回答No.3

根本的な話ですが・・・ 罫線引くのにそんなに時間かかりますか? 試した限りでは、質問のプログラムは一瞬で終わりますが・・・ 他にも計算式などがあるようなので、新規のブックで質問のプログラムだけを動かしても、秒単位で時間がかかりますか? 各処理(Range(??).Selectから次のRange(??).Selectまでの部分、再計算も1つとして)だけにして実行して、どの部分が一番時間がかかりますか? または、質問のプログラムのコメントの位置でmsgboxすると、ちょうどいいかもしれません。 Range("C3:E22,H3:J22").Select ・・・ ' ここまでは、共通の動作 MsgBox "ここまでは、共通の動作" Range("A1").Select ActiveCell.FormulaR1C1 = "20" ' 問題数に応じて、数字を変更 MsgBox "問題数に応じて、数字を変更" Range("C3:E22,H3:J22").Select ・・・ ' 四線を消去 MsgBox "四線を消去" Range("D3:D4").Select Calculate ' 再計算完了 MsgBox "再計算完了" として、どの部分が一番時間がかかりますか? もしかしたら、見えない何かがありませんか? Sub check() MsgBox Shapes.Count End Sub とかしたら、いくつ表示しますか? いらないshapeがあるようなら、消してみてはどうでしょうか? 下は全部のシェープを消します。 Sub check() Shapes.SelectAll Selection.Delete End Sub その状態ではどれくらい時間がかかりますか?

add0804
質問者

補足

今、もう一度動作確認したら、10秒弱に縮まっていました。 なぜ短縮されたかわからないですが・・・(昨日の夜から何も変えてないのに) 一般的にこの10秒弱の動作は遅いものなのでしょうか? あと、 Sub check() MsgBox Shapes.Count End Sub これやってみましたが、エラーになりました・・・

その他の回答 (7)

回答No.8

サンプルではそれほど時間がかからない(遅い所でも1秒前後)のですが、10秒(数秒)単位で時間がかかりますか? 細かい事は別にして、それほど高速化はできないみたいでした。

add0804
質問者

お礼

わかりました。 何度もありがとうございました。

回答No.7

質問のプログラムの各処理にがかかる時間を表示します。 最初のApplication.ScreenUpdatingは、ある場合と無い場合で違いを見てください。 特に時間がかかっている処理が無くてApplication.ScreenUpdatingのある場合が早いなら、何か全体を遅くしている表示関係の処理があるので、新たなブックに作り直すのが一番いいと思います。 'Application.ScreenUpdating = False '実行する場合としない場合でトータル時間の差を見る Dim msg As String Dim t As Single Dim tt As Single tt = Timer t = Timer Range("C3:E22,H3:J22").Select Selection.Font.ColorIndex = 2 Selection.Interior.ColorIndex = 2 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 Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone ' ここまでは、共通の動作 msg = msg & "ここまでは、共通の動作=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Range("A1").Select ActiveCell.FormulaR1C1 = "20" ' 問題数に応じて、数字を変更 msg = msg & "問題数に応じて、数字を変更=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Range("C3:E22,H3:J22").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.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With ' 罫線を引く msg = msg & "罫線を引く=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Range("D3:E22,I3:J22").Select Selection.Font.ColorIndex = 1 ' 文字を黒くする msg = msg & "文字を黒くする=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Range("C3:C22,H3:H22").Select Selection.Interior.ColorIndex = 16 ' セルをグレーにする msg = msg & "セルをグレーにする=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Rows("3:18").Select Selection.RowHeight = 31.5 ' セルの幅を指定 msg = msg & "セルの幅を指定=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer ActiveSheet.PageSetup.PrintArea = "$B$1:$K$25" ' 印刷範囲を指定 msg = msg & "印刷範囲を指定=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Range("U3:U42").Select Selection.ClearContents ' 四線を消去 msg = msg & "四線を消去=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer Range("D3:D4").Select Calculate ' 再計算完了 msg = msg & "再計算完了=" & Format(Timer - t, "0.00") & vbCrLf: t = Timer msg = msg & "TotalTime=" & Format(Timer - tt, "0.00") Application.ScreenUpdating = True MsgBox msg

add0804
質問者

補足

教えていただいたものを新しいブックで試した結果、 両方とも差はありませんでした。 また、偏って時間がかかることもありませんでした。 自分が作ったサンプルをのせたので、見てもらえますか? http://briefcase.yahoo.co.jp/bc/add0804/lst?.dir=/%a5%de%a5%a4%a5%c9%a5%ad%a5%e5%a5%e1%a5%f3%a5%c8

回答No.6

>MsgBox ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas).Count >を試した結果、182でした。しかし、どれも必要な数式なので >限界ですかね。 どんな数式かわかりませんが、182個程度ではそんなに時間はかからないと思います。 F8で、1ステップずつ実行してみた結果はどうだったのでしょうか? 納得されたならいいですが、たぶん原因は違う所にあると思います。

add0804
質問者

補足

F8をやってみた結果、どの過程でも1秒未満ですぐ動作しました。 しかし、全体を通して実行するとやはり10秒程度かかります。 (1つ1つの動作を合計しても2秒はかかりませんでした) どうしてですかね?・・・

回答No.5

ANo.4です。 >確かに新しいブックだと1秒くらいでできました。 >ってことは遅い原因はエクセルの関数ってことですか? F8で、1ステップずつ実行してみてください。 異常に時間がかかる部分がわかるはずです。 見えない大量のシェープがあるのかと思いましたが、35個程度ならたぶん問題なと思います。 最後の再計算が遅いのかもしれませんが、質問の内容から10秒もかかるような作業になるとは思えません。 ちなみに、 MsgBox ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas).Count で、シート中の計算式のセルの個数がわかるので、極端に多いようならそのせいかもしれません。 >あと、「MsgBox ActiveSheet.Shapes.Count」やってみたら >「35」って出ました。 >これはどういうことですか? テキストボックスや図形などが35個あるということです。 表示関係が極端に遅くなったり、ファイルサイズが巨大になる原因になるようです。 35個程度なら問題はないと思いますが、覚えがないなら、下記で削除してください。 ActiveSheet.Shapes.SelectAll Selection.Delete 表示関係で遅い場合、最初に Application.ScreenUpdating = False 最後に Application.ScreenUpdating = True を入れると、速くなる場合もあります。 どうしても遅くなる理由がわからない場合は、新しいシートに作り直して見るというのが一番早いかもしれません。

add0804
質問者

お礼

長時間ありがうございました。 細かく教えていただいたおかげで、徐々にわかってきました。 最後に、 MsgBox ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas).Count を試した結果、182でした。しかし、どれも必要な数式なので 限界ですかね。

回答No.4

ANo.3です。 >一般的にこの10秒弱の動作は遅いものなのでしょうか? 新しいブックに質問のプログラムだけだと、1秒はかからないと思います。(たぶん) 新しいブックで、質問のプログラムを動かしたらどうなりますか? >Sub check() >MsgBox Shapes.Count >End Sub >これやってみましたが、エラーになりました・・・ すみません、以下ではどうでしょうか? MsgBox ActiveSheet.Shapes.Count または、シート名がSheet1なら MsgBox Worksheets("Sheet1").Shapes.Count

add0804
質問者

補足

確かに新しいブックだと1秒くらいでできました。 ってことは遅い原因はエクセルの関数ってことですか? あと、「MsgBox ActiveSheet.Shapes.Count」やってみたら 「35」って出ました。 これはどういうことですか?

  • hana-hana3
  • ベストアンサー率31% (4940/15541)
回答No.2

>ちなみに、「根本的に書き換えてもっと早く動作する」なんてことはできるんですか? 仕様によります。 1.最初の初期化は必用なのか? 2.罫線の設定は必用なのか? 3.セル幅(高さ)の設定は必用なのか? 4.印刷範囲の設定は必用なのか? 5.四線を消去は必用なのか? 6.再計算は必用なのか? 無駄(不要)と思う部分を削除するか、別の方法で行うかの問題になります。 何が必用で、何が不要なのかがはっきりしないので何とも言えませんし、何度も実行するマクロでも無いとおもいます。 (1度実行すれば目的は達成される)

add0804
質問者

補足

まず、このマクロを使っているシートについて説明します。 英単語の小テスト(印刷して配布)を作るためのものです。 問題数は5問・10問・15問・20問の4パターン作る予定です。 問題はエクセルの関数でランダムに表示されるようになっています。 この前提で 1、初期化は問題数によって罫線や表示している問題数が違うため   必要です。 2、同上。 3、問題数によってセルの幅が変わる(1枚のシートに入るようにする)   ため必要。 4、問題数によって印刷範囲が変わるため、必要。 5、「四線」とは英語の4線のことですが、ワードアートのリンクで   表示されるようにしているため、解答を表示するときに必要。 6、再計算は、問題が勝手に変わらないように、手動で計算という   設定になっているため、問題数が変わったときに   シート内の関数を反映させるためには必要。 いちいち問題を作るのが面倒なので、いっそのことマクロでと 思ったのですが、動作が遅いのは仕方がないのですかね? ちなみに修正したら15秒までは早くなりました。 宜しくお願いします。

  • hana-hana3
  • ベストアンサー率31% (4940/15541)
回答No.1

短くするならこれだけ。 With Range("C3:E22,H3:J22") .Font.ColorIndex = 2 .Interior.ColorIndex = 2 .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone .Borders(xlEdgeLeft).LineStyle = xlNone .Borders(xlEdgeTop).LineStyle = xlNone .Borders(xlEdgeBottom).LineStyle = xlNone .Borders(xlEdgeRight).LineStyle = xlNone .Borders(xlInsideVertical).LineStyle = xlNone .Borders(xlInsideHorizontal).LineStyle = xlNone .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone End With ' ここまでは、共通の動作 Range("A1") = 20 ' 問題数に応じて、数字を変更 With Range("C3:E22,H3:J22") .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlEdgeTop).LineStyle = xlContinuous .Borders(xlEdgeBottom).LineStyle = xlContinuous .Borders(xlEdgeRight).LineStyle = xlContinuous .Borders(xlInsideVertical).LineStyle = xlContinuous .Borders(xlInsideHorizontal).LineStyle = xlContinuous .Font.ColorIndex = 1 ' 文字を黒くする .Interior.ColorIndex = 16 ' セルをグレーにする End With Rows("3:18").RowHeight = 31.5 ' セルの幅を指定 ' 罫線を引く ActiveSheet.PageSetup.PrintArea = "$B$1:$K$25" ' 印刷範囲を指定 Range("U3:U42").ClearContents ' 四線を消去 Range("D3:D4").Select Calculate ' 再計算完了 >このVBAは慣れている人から見ると何点くらいですか?(感覚で結構です) 記録マクロそのままみたいですから・・・

add0804
質問者

お礼

ありがとうございました! 動作時間が30秒→20秒に短縮されました!! ちなみに、「根本的に書き換えてもっと早く動作する」 なんてことはできるんですか?

関連するQ&A

  • 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

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

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (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

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

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

  • ,最終行から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

  • エクセル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

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

    マクロ記録をつかって、選択している範囲に黒の格子を付けるマクロをボタンを作成しました。 (質問の下に、記録した内容を添付します) 実際に、格子を作るのには不要な行がたくさんあると思うのですが、不要な箇所をどう見つければいいのか教えてください。 個人的には、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

  • このマクロコードをダイエットするには?

    罫線を引き、配色し、文字に色を付ける。 このコードをどのようにダイエットすればよいのでしょうか? 罫線のマクロはこんなに大きいのですか? Withの使い方がよくわかりません。 よろしくお願い致します。 ------------ Sub Test1() Range("B1:O14").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlHairline .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 With Selection.Interior .ColorIndex = 56 .Pattern = xlSolid End With Selection.Font.ColorIndex = 33 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に作成される よろしくお願いします

  • 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

  • エクセル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

専門家に質問してみよう