• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:VBA2010で罫線を引くマクロがエラー停止する)

VBA2010で罫線を引くマクロがエラー停止する

MARU4812の回答

  • MARU4812
  • ベストアンサー率43% (196/452)
回答No.2

エラー原因と言いながら、エラーメッセージ書いてない ように見えるんだけど?

Comarch18
質問者

お礼

MARU4812様回答ありがとうございます。 MARU4812のご指摘通りエラーメッセージ等の情報の記載がなく分かりにくい文章になっていました。再度補足にエラーメッセージ等の情報を記載いたしましたのでご協力をお願い致します。

Comarch18
質問者

補足

MARU4812様、imogasi様回答ありがとうございます。 説明不足で申し訳ありません再度状況について書かせていただきますのでご協力をお願い致します。 1.エラーメッセージ 実行時エラー”11004” Border クラスのWgightプロパティ確定できません 2.マクロの停止位置及び各パラメータの値 1)マクロ停止位置 上記のエラーが出たときに”デバック”を押すと下記の箇所でエラーになって停止しています。 Sub 太罫線(開始行, 開始列, 終了行, 終了列) Range(Cells(開始行, 開始列), Cells(終了行, 終了列)).Select Selection.Borders(xlEdgeLeft).Weight = xlThick   ← この箇所でマクロは停止しています。 各パラメータの値を確認すると下記の様な値になっていました。 開始行:6 開始列:2 終了行:9 終了列:10 Subマクロについては、他でも使用しています。(メインのFor分で1回目で正しく動作しているので記載間違いは考えにくいです) 3.確認した事 上記のマクロは、決められた範囲のセルに太線の罫線を引くマクロです。 このマクロと同じことをエクセル上と行うには、”右クリック” ⇒ ”セルの書式設定”⇒"罫線”と同じことにを行うマクロです。 エラーになる箇所で”右クリック”⇒”セルの書式設定”を行いましたが”罫線”等の表示画面が出て来ません。 (浅はかな私の知識では、セルに何かの保護が掛けられて罫線が引かれないためにエラー停止したと考えました。) 但し、マクロ等のこのセルに保護を掛けてもいませんし、エラーになる前では”罫線”の画面が出て来ました。 以上が私が行った解析です。 拙い説明で申し訳ありませんがご協力をお願い致します。

関連するQ&A

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

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

    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

  • 選択したセルに罫線を引くマクロ

    お世話になっております。 業務上必要になり(効率化が図れるので)選択したセルにバツの罫線を引くマクロを作成しています。 まだまだ自分のレベルが低いので、マクロ記録を参考に、下記のようなマクロを作りました。 Sub バツ罫線マクロ()  ActiveCell.Select   With Selection.Borders(xlDiagonalDown)     .LineStyle = xlContinuous     .Weight = xlHairline     .ColorIndex = xlAutomatic   End With   With Selection.Borders(xlDiagonalUp)     .LineStyle = xlContinuous     .Weight = xlHairline     .ColorIndex = xlAutomatic   End With End Sub 試してみると、選択したセルに対してバツの罫線が引かれました。 やった!と思ったのもつかの間、Ctrlキーでセルを複数選択してマクロを実行すると最後にアクティブにしたセルしか罫線が引かれないではありませんか・・・。 ネットなどで調べてみると、Ctrlキーで複数選択してもアクティブセルというのはそのうちの一つだけというような記述がありました・・・。 そして、ここで行き詰ってしまいました。 お伺いしたいのは2つあります。 ・どうすればCtrlキーで選択した全てのセルにこの罫線を引くマクロを適用させる事が出来ますか? ・変数などを用いたもっとレベルの高いコードの書き方だと、どのように記述されますか?後学のためにレベルの高い人の記述方法と比較させてもらって勉強させてもらいたいと思っています。 よろしくお願いします。

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

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (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マクロを用いて罫線作成をしています。 前任者のアレンジを頼まれたのですが前任者に連絡が取れず困っています。 表の特徴は以下のようになります。 ・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行目まで使用することは分かっているのですが… どうかご助力お願いします。

  • エクセルのマクロ

    縦方向に連続したセルを選択状態にした時に、選択セルを基点として右横方向に連続してデータが入力されているセルを罫線で囲う、という処理をマクロで実行したいのですが。 例えば、以下の図で黒丸がデータ入力セルであるとすると、C1~C4を選択状態にして実行すると、C1~D1とC2~F2とC4が罫線で囲われます。  AB CD EFG 1○●●●○○○ 2●●●●●●○ 3○○○○○○○ 4●●●○○○○ 以下のマクロを実行すると、上図の3行目と4行目のところがかなり余計に罫線が引かれてしまいます。どう修正すればいいでしょうか? Sub test1()  yc = Selection.Rows.count  aa = ActiveCell.Address  For y = 0 To yc - 1  ActiveCell.Offset(y, 0).Select  Range(ActiveCell, ActiveCell.End _       (xlToRight)).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    Range(aa).Select   Next y 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(Excel)セルの罫線について・・・

    VBAで、セルに罫線をつけたいのですが、選択範囲の外枠だけに罫線を ひきたいのに、選択範囲内全ての罫線がひかれてしまいます。 以下のようなものを実行しました。 Public sub Sample() Range("A1:C3").Select With Selection .BorderAround .Borders.ColorIndex = 1 '線の色を黒にする .Borders.Weight = xlThin '線を細い線にする End With End Sub どうしたら、選択範囲の外枠だけに線をひけるでしょうか? よろしくお願いします。

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