エクセル:シートを切り替えずに別シート上の操作をする

このQ&Aのポイント
  • エクセルでシートを切り替えずに別のシート上の操作を行う方法について質問があります。
  • 特定の条件下で、シート1に入力した値を基に、周囲のセルの罫線の色を変更するコードを作成したいです。
  • また、同じ操作を別のシート(Sheet2)にも行いたいが、シートを切り替えずに実行する方法を知りたいです。
回答を見る
  • ベストアンサー

エクセル:シートを切り替えずに別シート上の操作をする

タイトルが正しいかどうか疑問ですが。 シート[Sheet1]にて値を入力したアドレス(の行番号と列番号)を取得し、 その周囲のセルの罫線の色を赤(3)から灰色(15)に置換するコードを作っています。 Sheet1のコードには、 Private Sub Worksheet_Change(ByVal Target As Range)  AAA Target End Sub とだけ書き、入力があったらプロシージャAAAへTargetを持って飛びます。 Sub AAA(ByVal Target As Range)  Dim M_Row As Integer  Dim M_Clm As Integer  Dim Y As Range  M_Row = Target.Row  M_Clm = Target.Column  For Each Y In Worksheets("Sheet1").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5))   With Y    If .Borders(xlEdgeTop).ColorIndex = 3 Then .Borders(xlEdgeTop).ColorIndex = 15    If .Borders(xlEdgeLeft).ColorIndex = 3 Then .Borders(xlEdgeLeft).ColorIndex = 15    If .Borders(xlEdgeBottom).ColorIndex = 3 Then .Borders(xlEdgeBottom).ColorIndex = 15   End With  Next End Sub ここまでは正常に動きます。 この後に、アクティブでないシート[Sheet2]の同じセル範囲にある罫線の色も同じように置換したいので、 上記コードに続けて、以下のように書きました。 Sub AAA(ByVal Target As Range)  Dim M_Row As Integer  Dim M_Clm As Integer  Dim Y As Range  M_Row = Target.Row  M_Clm = Target.Column  For Each Y In Worksheets("Sheet1").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5))   With Y    If .Borders(xlEdgeTop).ColorIndex = 3 Then .Borders(xlEdgeTop).ColorIndex = 15    If .Borders(xlEdgeLeft).ColorIndex = 3 Then .Borders(xlEdgeLeft).ColorIndex = 15    If .Borders(xlEdgeBottom).ColorIndex = 3 Then .Borders(xlEdgeBottom).ColorIndex = 15   End With  Next  For Each Y In Worksheets("Sheet2").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5))   With Y    If .Borders(xlEdgeTop).ColorIndex = 3 Then .Borders(xlEdgeTop).ColorIndex = 15    If .Borders(xlEdgeLeft).ColorIndex = 3 Then .Borders(xlEdgeLeft).ColorIndex = 15    If .Borders(xlEdgeBottom).ColorIndex = 3 Then .Borders(xlEdgeBottom).ColorIndex = 15   End With  Next End Sub これだと、  For Each Y In Worksheets("Sheet2").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5)) の部分で失敗します。 この1行前に、  Sheets("Sheet2").Select と入れてやると正常に動作するのですが、 シートを切り替えずにやりたいと思っています。 可能でしょうか? 以下のように、 実行後にSheet1に戻し、 それらを Application.ScreenUpdating = False Application.ScreenUpdating = True で挟むことで、見た目はシートを切り替えずに実行できるのですが、 実際にこのコードを組み込んでいるシートはシート上にあるデータが多いためか(600行×100列程度)、 全く同じコードを実行しても一瞬画面がチラついてしまいます。 (新規Bookで同じコードを組み込んで、何行かに罫線を引いただけのシートだと全くチラつかなかったので、 シート上のデータが多いせいじゃないかと思いました) Sub AAA(ByVal Target As Range)  Dim M_Row As Integer  Dim M_Clm As Integer  Dim Y As Range  M_Row = Target.Row  M_Clm = Target.Column  For Each Y In Worksheets("Sheet1").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5))   With Y    If .Borders(xlEdgeTop).ColorIndex = 3 Then .Borders(xlEdgeTop).ColorIndex = 15    If .Borders(xlEdgeLeft).ColorIndex = 3 Then .Borders(xlEdgeLeft).ColorIndex = 15    If .Borders(xlEdgeBottom).ColorIndex = 3 Then .Borders(xlEdgeBottom).ColorIndex = 15   End With  Next  Application.ScreenUpdating = False  Sheets("Sheet2").Select  For Each Y In Worksheets("Sheet2").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5))   With Y    If .Borders(xlEdgeTop).ColorIndex = 3 Then .Borders(xlEdgeTop).ColorIndex = 15    If .Borders(xlEdgeLeft).ColorIndex = 3 Then .Borders(xlEdgeLeft).ColorIndex = 15    If .Borders(xlEdgeBottom).ColorIndex = 3 Then .Borders(xlEdgeBottom).ColorIndex = 15   End With  Next  Sheets("Sheet1").Select  Application.ScreenUpdating = True End Sub よろしくお願いします。

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

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

> For Each Y In Worksheets("Sheet2").Range(Cells(M_Row - 2, M_Clm), Cells(M_Row + 1, M_Clm + 5)) のRangeはSheet2を参照しているのに、CellsはこのままではActiveSheetすなわちSheet1を参照することになるので、当然オブジェクト定義エラーになる。 ここは、長くなるが、 For Each Y In Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(M_Row - 2, M_Clm), Worksheets("Sheet2").Cells(M_Row + 1, M_Clm + 5)) としなければならない。 with を使え。

tktk1228
質問者

お礼

回答ありがとうございます。 With Worksheets("Sheet2") End With で挟んで望んだような動作となり、解決しました。 Cellsの説明で「Objectの指定を省略したときにはアクティブシー トが対象となります。」という文も発見しました。 ありがとうございました。

関連するQ&A

  • エクセルで選択中の列や行を見やすくしたい

    タイトルのとおり、選択中の列や行の色が一列全部変るように したいと思い調べ、以下のVBEコードを見つけたんですが Public m Private Sub Worksheet_SelectionChange(ByVal Target As Range)  If m <> 0 Then   Range(Cells(m, 1), Cells(m, 256)).Interior.ColorIndex = xlNone  End If  Range(Cells(Target.Row, 1), Cells(Target.Row, 256)).Interior.ColorIndex = 6  m = Target.Row 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 「文字が入っていたら、上下に線を引く」

    困っています。どなたか教えてください。 下記のように作成しましたが、 A列に文字が入っていたら、上下に線を引くというプロシージャにしたいと思っています。 If Cells(c, 4) = "" Then Range(Cells(c, 1), Cells(c, 6)).Select With Selection.Borders(xlEdgeBottom) .LineStyle = xlDot .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With bolFlg = True Else まだまだ続きますが・・・・。 よろしくお願いいたします。

  • エクセルで、こうやっても反応なしです。

    よろしくお願いします。以下のように組んで見ました。 Private Sub Worksheet_Change(ByVal Target As range) Dim clm As Integer Dim row As Integer clm = Target.Column row = Target.row If Worksheets("発注指示").Cells(row, clm) = "不足" Then MsgBox "在庫不足", vbOKOnly, "注意" End If 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行目まで使用することは分かっているのですが… どうかご助力お願いします。

  • セルの選択でその行に色を付けたい

    横に長いデータがあり、その1つのセルを選択するとその行全体に色が付くようにしたいのです。過去の質問で以下のようなものを見つけましたが、問題はその場合、通常のコピー→貼り付けができない点です。 その辺を問題なく行える方法はないでしょうか? よろしくお願いいたします。 Public m, n Private Sub Worksheet_SelectionChange(ByVal Target As Range) If m <> 0 Then Range(Cells(m, 1), Cells(m, 256)).Interior.ColorIndex = n End If m = Target.Row n = Target.Interior.ColorIndex Range(Cells(Target.Row, 1), Cells(Target.Row, 256)).Interior.ColorIndex = 6 End Sub

  • EXCEL VBA 早く処理をする

    よろしくお願いします 下の構文を標準モジュールに書き込み、callで実行しているのですが 処理に時間がかかります。 処理を早くする方法と構文の簡素化のご教示をお願いします。 Application.ScreenUpdating = False For i = 1 To 12 With Worksheets(i) .Select LastRow = .Range("A150").End(xlUp).Row + 1 .Range("A8:G" & LastRow).Sort Key1:=Range("A8"), order1:=xlAscending .Range("G8:G" & LastRow - 1).Formula = "=G7+E8-F8" LastRow = .Range("A150").End(xlUp).Row + 1 .Range("A" & LastRow).Select Dim EndRow As Long EndRow = .Range("A" & Rows.Count).End(xlUp).Row Cells(Rows.Count, 1).End(xlUp).Offset(1, 3) = .Name & "合計" Cells(Rows.Count, 1).End(xlUp).Offset(1, 4) = Application.WorksheetFunction.Sum(Range("E7:E" & EndRow)) Cells(Rows.Count, 1).End(xlUp).Offset(1, 5) = Application.WorksheetFunction.Sum(Range("F7:F" & EndRow)) Cells(Rows.Count, 1).End(xlUp).Offset(2, 3) = "前月繰越" Cells(Rows.Count, 1).End(xlUp).Offset(2, 4) = .Range("G7") Cells(Rows.Count, 1).End(xlUp).Offset(2, 5) = "" Cells(Rows.Count, 1).End(xlUp).Offset(3, 4) = "" Cells(Rows.Count, 1).End(xlUp).Offset(3, 3) = "次月繰越" Cells(Rows.Count, 1).End(xlUp).Offset(4, 3) = "合計" Cells(Rows.Count, 1).End(xlUp).Offset(3, 5) = Cells(Rows.Count, 1).End(xlUp).Offset(2, 4) + Cells(Rows.Count, 1).End(xlUp).Offset(1, 4) - Cells(Rows.Count, 1).End(xlUp).Offset(1, 5) Cells(Rows.Count, 1).End(xlUp).Offset(4, 4) = Cells(Rows.Count, 1).End(xlUp).Offset(2, 4) + Cells(Rows.Count, 1).End(xlUp).Offset(1, 4) Cells(Rows.Count, 1).End(xlUp).Offset(4, 5) = Cells(Rows.Count, 1).End(xlUp).Offset(3, 5) + Cells(Rows.Count, 1).End(xlUp).Offset(1, 5) Cells(Rows.Count, 1).End(xlUp).Offset(1, 6) = "" Cells(Rows.Count, 1).End(xlUp).Offset(2, 6) = "" Cells(Rows.Count, 1).End(xlUp).Offset(3, 6) = "" Cells(Rows.Count, 1).End(xlUp).Offset(4, 6) = Cells(Rows.Count, 1).End(xlUp).Offset(0, 6) .Range("C7").End(xlDown).Select Selection.Offset(4, 2).Borders(xlEdgeTop).Weight = xlHairline Selection.Offset(4, 2).Borders(xlEdgeBottom).LineStyle = xlDouble Selection.Offset(4, 3).Borders(xlEdgeTop).Weight = xlHairline Selection.Offset(4, 3).Borders(xlEdgeBottom).LineStyle = xlDouble Selection.Offset(4, 4).Borders(xlEdgeTop).Weight = xlHairline Selection.Offset(4, 4).Borders(xlEdgeBottom).LineStyle = xlDouble Selection.Offset(0, 2).Borders(xlEdgeTop).Weight = xlHairline Selection.Offset(0, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Selection.Offset(0, 2).Borders(xlEdgeBottom).Weight = xlThin Selection.Offset(0, 3).Borders(xlEdgeTop).Weight = xlHairline Selection.Offset(0, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Selection.Offset(0, 3).Borders(xlEdgeBottom).Weight = xlThin Selection.Offset(0, 4).Borders(xlEdgeTop).Weight = xlHairline Selection.Offset(0, 4).Borders(xlEdgeBottom).LineStyle = xlContinuous Selection.Offset(0, 4).Borders(xlEdgeBottom).Weight = xlThin End With Next i Application.ScreenUpdating = True

  • メッセージボックスについて。

    上司に言われた締め切りがあと4日になってしまいました。 在庫管理で在庫残高シートに次のプログラムをはってみました。 Private Sub Worksheet_Change(ByVal Target As range) Dim clm As Integer '変化したセルの列 Dim row As Integer '変化したセルの行 Dim counter As Integer '不足数 clm = Target.Column row = Target.row If Worksheets("在庫残高").Cells(row, clm) < Worksheets("在庫限界入力").Cells(row, clm) And Worksheets("在庫残高").Cells(row, clm) > 0 Then counter = Worksheets("在庫限界入力").Cells(row, clm) - Worksheets("在庫残高").Cells(row, clm) MsgBox counter & "本在庫不足", vbOKOnly, "注意" Else If Worksheets("在庫残高").Cells(row, clm) < 0 Then MsgBox "在庫がありません", vbOKOnly, "警告" End If End If 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で罫線挿入

    マクロの記録で下記のように記録されたものを簡潔にまとめるにはどのように記述したらいいでしょうか? 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

専門家に質問してみよう