プロが教える店舗&オフィスのセキュリティ対策術

お世話になります
excelシート イベントでセルの値が変わった時にマクロが実行されるVBAを作成していて、
1行毎の入力作業はうまくいくのですが、式の入っていないセルを複数行をコピペ、
例えばA,Bセルの値が同じものが5件ほどあった場合、最初の入力のものをしたにドラッグして
貼り付けると、画面がフリーズして強制終了せざる負えなくなります。

エラーを回避する方法をご教示お願いいたします。以下VBAの内容です。

Dim sh1 As Worksheet
Dim i As Integer

Private Sub Worksheet_Calculate()

'detailに指標をセット
i = 9
シートをworkエリアにセット
Set sh1 = Worksheets(4)
'カードルシート,2ページ(予備)まで指標を回す
For i = 9 To 66
'画面ちらつき防止
Application.ScreenUpdating = False
'2ページ(予備)目ヘッダーは処理しない
If i < 33 Or i > 41 Then
'サンプル番号が入力されている時
If sh1.Cells(i, "E") <> "" Then
'サンプル年月が入力されている時
If IsError(sh1.Cells(i, "K")) <> True Then
'基準年月 >= サンプル年月 の時
If sh1.Cells(7, "O") >= sh1.Cells(i, "K") Then
'次回サンプル年月 <= 当年月 の時
If sh1.Cells(i, "Q") <= sh1.Cells(8, "O") Then
sh1.Cells(i, "M") = "出荷禁止"
'該当行を赤色で塗りつぶし
With sh1
.Cells(i, "A").Interior.ColorIndex = 3
.Cells(i, "B").Interior.ColorIndex = 3
.Cells(i, "C").Interior.ColorIndex = 3
.Cells(i, "D").Interior.ColorIndex = 3
.Cells(i, "E").Interior.ColorIndex = 3
.Cells(i, "F").Interior.ColorIndex = 3
.Cells(i, "G").Interior.ColorIndex = 3
.Cells(i, "H").Interior.ColorIndex = 3
.Cells(i, "I").Interior.ColorIndex = 3
.Cells(i, "J").Interior.ColorIndex = 3
.Cells(i, "K").Interior.ColorIndex = 3
.Cells(i, "L").Interior.ColorIndex = 3
.Cells(i, "M").Interior.ColorIndex = 3
End With
Else
'次回サンプル年月 <= 当年月 でない時
sh1.Cells(i, "M") = "OK"
End If
End If
End If
Else
'該当行を無色で塗りつぶし
With sh1
.Cells(i, "A").Interior.ColorIndex = 0
.Cells(i, "B").Interior.ColorIndex = 0
.Cells(i, "C").Interior.ColorIndex = 0
.Cells(i, "D").Interior.ColorIndex = 0
.Cells(i, "E").Interior.ColorIndex = 0
.Cells(i, "F").Interior.ColorIndex = 0
.Cells(i, "G").Interior.ColorIndex = 0
.Cells(i, "H").Interior.ColorIndex = 0
.Cells(i, "I").Interior.ColorIndex = 0
.Cells(i, "J").Interior.ColorIndex = 0
.Cells(i, "K").Interior.ColorIndex = 0
.Cells(i, "L").Interior.ColorIndex = 0
.Cells(i, "M").Interior.ColorIndex = 0
.Cells(i, "M") = ""
End With
End If
End If

Next i

End Sub

A 回答 (1件)

Application.ScreenUpdating = False


とあるので、
Application.ScreenUpdating = True
をNextの下あたりに入れるべきです。

また Application.ScreenUpdating = False はFor文の前に行って下さい。
無駄に繰り返されています。

余談ですが、
.Cells(i, "A")

.Cells(i, "M")
の部分はRangeを使うとスッキリします。
Range(Cells(i, 1), Cells(i, 13)).Interior.ColorIndex = 3
    • good
    • 0
この回答へのお礼

Rugi_sasakiさん
お世話になります。
早速の回答ありがとうございます。

>Application.ScreenUpdating = False はFor文の前に行って下さい。
>Range(Cells(i, 1), Cells(i, 13)).Interior.ColorIndex = 3
うまくいきました。ありがとうございます。

 しかしコピペすると画面はフリーズしたままで強制終了するしかないです。
処理対象のセル以外のイベント発生を抑えるしかないでしょうか?
それとも異なるイベント、たとえばダブルクリックとか?
宜しくおねがいします。m(__)m

お礼日時:2013/10/25 12:20

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!