アプリ版:「スタンプのみでお礼する」機能のリリースについて

エクセル自動実行のマクロを作成中にうまく動かないので
サンプルをコピーして、変更してみたのですが
そのサンプルも動いません、マクロとは違う何か悪いのでしょうか?

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim intColor As Integer
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B2:B11")) Is Nothing Then Exit Sub
Select Case Target.Value
Case Is <= 20
intColor = 3
Case 21 To 40
intColor = 46
Case 41 To 60
intColor = 9
Case 61 To 80
intColor = 10
Case Is > 80
intColor = 5
End Select
Target.Font.ColorIndex = intColor
Application.EnableEvents = True
End Sub

A 回答 (2件)

こんばんは。



Application.EnableEvents = False
が実行されたあとにエラーが発生し、
Application.EnableEvents = True
が実行されることなく、Falseになったままに
なっているのでは無いでしょうか?

以下を実行してTRUEに戻してみてください。
Sub test()
 Application.EnableEvents = True
End Sub
    • good
    • 1
この回答へのお礼

動くようになりました
ありがとうございます

お礼日時:2009/12/03 21:00

#1です。



コードを良く見ると、
Application.EnableEvents = False
が実行されたあとに
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B2:B11")) Is Nothing Then Exit Sub
の2行で条件一致すると
Application.EnableEvents = True
が実行されずに
Exit Sub
されます。

以下のようにしたらどうでしょうか?
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim intColor As Integer
  If Target.Count > 1 Then Exit Sub
  If Intersect(Target, Range("B2:B11")) Is Nothing Then Exit Sub

  Select Case Target.Value
    Case Is <= 20
      intColor = 3
    Case 21 To 40
      intColor = 46
    Case 41 To 60
      intColor = 9
    Case 61 To 80
      intColor = 10
    Case Is > 80
      intColor = 5
  End Select

  Application.EnableEvents = False
    Target.Font.ColorIndex = intColor
  Application.EnableEvents = True
End Sub

ただ、ColorIndexを変更してもWorksheet_Changeイベントは発生しない
ようなので、
  Application.EnableEvents = False
  Application.EnableEvents = True
で囲む必要は無いような気がします。。。
    • good
    • 0
この回答へのお礼

いらないですね
アドバイスありがとうございます

お礼日時:2009/12/03 21:01

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

このQ&Aを見た人はこんなQ&Aも見ています