• ベストアンサー

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

表題どおりなのですが、別シートに罫線がかけません。 例えば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

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

  • ベストアンサー
  • popesyu
  • ベストアンサー率36% (1782/4883)
回答No.1

おそらくシートモジュールにプログラムを書いてるんですよね? その場合、特に指定せずにRangeやCellsを使うとそれはそのシートモジュールのWorksheetを参照することになるという決まりごとがあります。これが標準モジュールの方ならとくに指定が無ければActiveSheetとするという決まりごとがあります。 つまり下記の部分は ActiveSheet.Range(Cells(4, i), Cells(30, i)) ↓ ActiveSheet.Range(Worksheets("sheet1").Cells(4, i), Worksheets("sheet1").Cells(30, i)) という認識になっているので正常に動作しません。もしシートモジュールの方に書くなら下のように正確に指定する必要があります。 ActiveSheet.Range(ActiveSheet.Cells(4, i), ActiveSheet.Cells(30, i)) 他の対策としては標準モジュールに書いたコードをCallで呼び出す方式にするという方法もあります。その場合だと「ActiveSheet」を省略しても正常に動作します。

a-k-a
質問者

お礼

お礼が遅くなってしまいました. 確かにシートモジュールに書いていました. 原因が分かりスッキリです. ありがとうございました.

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

その他の回答 (2)

  • imogasi
  • ベストアンサー率27% (4737/17068)
回答No.3

#1でご指摘の方法 Sheetのシートモジュール Private Sub CommandButton1_Click() Module1.aaa End Sub Module1に Public Sub aaa() Worksheets("Sheet2").Activate '.Selectでも同じ For i = 4 To Range("G30").Column ActiveSheet.Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThick ActiveSheet.Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next i End Sub でうまくいきました。 Private でなくPublicにすること。 ーーー Sub test01() 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 で実行してもエラー ーー Sheet1のシートモジュール Sub test01() For i = 4 To Range("G30").Column Worksheets("Sheet2").Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThin Worksheets("Sheet2").Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next End Sub でもエラー ーーー Sheet1のシートモジュール Sub test01() Worksheets("sheet2").Activate For i = 4 To Range("G30").Column Worksheets("Sheet2").Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThin Worksheets("Sheet2").Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next End Sub を実行でもエラー ーーー Sheet1のシートモジュール Private Sub CommandButton1_Click() Worksheets("sheet2").Activate For i = 4 To Range("G30").Column Worksheets("Sheet2").Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThin Worksheets("Sheet2").Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next End Sub でもエラー ーーー Private Sub CommandButton1_Click() Dim sh2 As Worksheet Set sh2 = Worksheets("Sheet2") sh2.Activate For i = 4 To Range("G30").Column sh2.Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThin sh2.Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next End Sub もエラー ーーー Sub test01() Dim sh2 As Worksheet Set sh2 = Worksheets("Sheet2") For i = 4 To sh2.Range("G30").Column sh2.Range(sh2.Cells(4, i), sh2.Cells(30, i)).Borders(xlLeft).Weight = xlThin sh2.Range(sh2.Cells(4, i), sh2.Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next End Sub はOK ーーーー #2のご回答 Private Sub CommandButton1_Click() With Worksheets("sheet2") For i = 4 To Range("G30").Column .Range(.Cells(4, i), .Cells(30, i)).Borders(xlLeft).Weight = xlThick .Range(.Cells(4, i), .Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next i End With End Sub はOK。 以上参考のため、報告します。 シートモジュールの中で、他のシートのあActivate はできないのかと思ったが、そうではなくSh2.をCell()の前にまでつけていなかったのが原因。 他シートを参照するなら、Range、Cellsの前に、徹底して Sh2.(またはWorksheets(”Sheet2”)をつけないといけないということでした。 Range(Cells(4, i), Cells(30, i)).のCellにはつけるのを忘れやすいですね。私も失敗した例を思いがしました。

a-k-a
質問者

お礼

お礼が遅くなってしまいました. まとめていただいてありがとうございます. 原因が分かりスッキリです. ありがとうございました.

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

> 「1004 Rangeメソッドは失敗しました。」 > ActiveSheet.Range(Cells(4, i), Cells(30, i)).Borders(xlLeft).Weight = xlThin 「ActiveSheet」が指し示すのが「Sheet2」で、 「Cells」が指し示すのが「Sheet1」だからではありませんか? > ActiveSheet.Range(ActiveSheet.Cells(4, i), ActiveSheet.Cells(30, i)).Borders(xlLeft).Weight = xlThin こうしてみるとか・・・ 一般に、「Active~」とか「Select~」は、不正確(想定外のもの)になることがあるので、 できるだけ使わないほうが良いですよ。 例えば、 ------------------------------------------------------------ With Worksheets("sheet2") For i = 4 To Range("G30").Column .Range(.Cells(4, i), .Cells(30, i)).Borders(xlLeft).Weight = xlThin .Range(.Cells(4, i), .Cells(30, i)).Borders(xlLeft).LineStyle = xlContinuous Next End With ------------------------------------------------------------ こうして、明示的に「Worksheets("sheet2")」を指定したほうが確実です。

a-k-a
質問者

お礼

お礼が遅くなってしまいました. 確かにシートモジュールに書いていました. 原因が分かりスッキリです. ありがとうございました.

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

関連するQ&A

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

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

  • ,最終行から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における罫線の色指定について

    エクセルVBAの初心者です。 使用機種はWindows VistaでExcel2007です。 後に示すコードではどうして("B4:H7")までの下罫線と ("C4:H8")までの左罫線が青色にならず、黒色のまま になるのでしょうか?("B4:H7")、("C4:H8")ともに 罫線の色は青色にしたいと思っています。 原因と対処方法をご存知の方ご教示願います。 画像も添付しておりますので、合わせてご参照ください。 Sub 日程表作成() Set WS1 = Worksheets("sheeT1") With WS1 .Range("B3") = "日" .Range("B3").Select End With Selection.AutoFill Destination:=Range("B3:H3") WS1.Range("B3:H3").Select With Selection.Borders(xlLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With With Selection.Borders(xlTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With With Selection.Borders(xlBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With With Selection.Borders(xlRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With '下線を二重線に変える Range("B3:H3").Select Selection.Borders(xlBottom).LineStyle = xlDouble WS1.Range("B3:H8").BorderAround Weight:=xlThick, _ ColorIndex:=5, LineStyle:=xlContinuous WS1.Range("B3:H8").RowHeight = 35 '日程表の中に横線を入れる WS1.Range("B4:H7").Select Selection.Borders(xlBottom).LineStyle = xlContinuous WS1.Range("B3:H8").BorderAround Weight:=xlThick, _ ColorIndex:=5, LineStyle:=xlContinuous '日程表の中に縦線を入れる WS1.Range("C4:H8").Select Selection.Borders(xlLeft).LineStyle = xlContinuous WS1.Range("B3:H8").BorderAround Weight:=xlThick, _ ColorIndex:=5, LineStyle:=xlContinuous '曜日のセルを塗りつぶす With WS1 .Range("B3:B8").Interior.ColorIndex = 38 .Range("C3:G8").Interior.ColorIndex = 19 .Range("H3:H8").Interior.ColorIndex = 19 .Range("B4").Activate End With 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

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

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

    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 どこのワークシートかを指定する場合はどうしたらいいのでしょうか?

  • 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

  • ブック内に会計仕分けの元帳シート(50シートほど)

    ブック内に会計仕分けの元帳シート(50シートほど) この各シート月締めのための二重罫線を引きたいです 日付列(A)をロールアップにて最終書き込みセルを探し、 そのセルに二重下線、列A~Gまでをマクロで全元帳シートに。 ? ループ処理のコードはだいたいわかります。 マクロ記録してみましたが、 A75(下記コードの場合)は、元帳シートにより異なるので、 任意(変数?)にしたい場合はどうすれば? ? 下記記録は、列A~Eのコードが書かれているのでしょうか? AとかBとか、Eが見当たらない!? ? Sub Macro1() Selection.End(xlUp).Select Range("A75:A75").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 = xlDouble .Weight = xlThick .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 Range("A1").Select 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 罫線 文字列

    職場で使う表を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行目まで使用することは分かっているのですが… どうかご助力お願いします。

このQ&Aのポイント
  • 外付けのドライブからフロッピーを読み込む方法についてお教えください。
  • フロッピーが正常に読み込めない問題が発生しています。解決策を教えてください。
  • NEC 121wareの使用方法について教えてください。
回答を見る