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

Excel2003 VBAにて条件付き書式のマクロを書きたいのですが、どうも上手くいきません。

1列おき(C列、E列、G列・・・)に条件付き書式を設定し、

条件は、
・セルの値が”0”より大きい場合はフォント”赤”で表示。
・セルの値が”0”より小さい場合はフォント”緑”で表示。

としたいと思いマクロを組んでみました。








Sub Color()

Dim j, j0
Dim x

Worksheets("sheet1").Active

Application.ScreenUpdating = False

j0 = 3
j = 300

For x = 3 To 100 Step 2

Range(.Cells(j0, x), .Cells(j, x)).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="0"
Selection.FormatConditions(1).Font.ColorIndex = 3
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="0"
Selection.FormatConditions(2).Font.ColorIndex = 10
Next

Application.ScreenUpdating = True

End Sub



デバックでステップインしていくと、ここで実行時エラー1004 アプリケーション定義またはオブジェクト定義のエラーです。がでます。
Selection.FormatConditions(1).Font.ColorIndex = 3


--------------------------------------------------------------------------------------
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="0"
Selection.FormatConditions(1).Font.ColorIndex = 3
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="0"
Selection.FormatConditions(2).Font.ColorIndex = 10
--------------------------------------------------------------------------------------
この間は、マクロの自動記録で書かれてるので間違ってはいないハズなのですが、、、


お知恵を下さい。

よろしくお願いします。

A 回答 (3件)

こんばんは!


せっかくコードをお考えですが、

ごくごく単純に・・・

Sub test()
Dim i, j As Long
For i = 3 To 300
For j = 3 To 100 Step 2
If Cells(i, j) > 0 Then
Cells(i, j).Font.ColorIndex = 3
ElseIf Cells(i, j) < 0 Then
Cells(i, j).Font.ColorIndex = 10
End If
Next j
Next i
End Sub

こんな感じではダメですか?m(__)m
    • good
    • 0
この回答へのお礼

同じ事をやるにも知識があるとマクロもシンプルになるのですね。
ありがとうございます。
勉強になりました。

お礼日時:2010/09/20 08:53

>マクロの自動記録で書かれてるので間違ってはいないハズなのですが、、、


いいえ、上の部分でWith ステートメントを削除したようです。

>Range(.Cells(j0, x), .Cells(j, x)).Select
ここが違っています。


Sub ConditionFormatColor()
 Dim i As Long, j As Long
 j= 300 '範囲の開始部分は、3 で、キメウチさせて頂きました。
 Application.ScreenUpdating = False
 With Worksheets("Sheet1")
  For i = 3 To 100 Step 2
   With Range(.Cells(3, i), .Cells(j , i))
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="0"
    .FormatConditions(1).Font.ColorIndex = 3
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="0"
    .FormatConditions(2).Font.ColorIndex = 10
   End With
  Next
 End With
 Application.ScreenUpdating = True
End Sub

なお、普通は、書式で行えるはずですが、同じ「緑色」の表現が出来ませんでした。
    • good
    • 0
この回答へのお礼

上記修正して頂いたマクロ実行し正常に動作しました。
ありがとうございます。

マクロを実行するシート選択の記述が必要だったんですね。

お礼日時:2010/09/20 09:01

Excel2003にて


 Selection.FormatConditions(1).Font.ColorIndex = 3
では、エラーは出ませんでした。
エラーが出るのは、
 Worksheets("sheet1").Active

 Range(.Cells(j0, x), .Cells(j, x)).Select

下記で動作しました。

Sub Color()

Dim j As Long
Dim j0 As Long
Dim x As Long


Worksheets("sheet1").Activate

Application.ScreenUpdating = False

j0 = 3
j = 300

For x = 3 To 100 Step 2

Range(Cells(j0, x), Cells(j, x)).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="0"
Selection.FormatConditions(1).Font.ColorIndex = 3
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="0"
Selection.FormatConditions(2).Font.ColorIndex = 10
Next

Application.ScreenUpdating = True

End Sub
    • good
    • 0
この回答へのお礼

yucco_chanさんに修正して頂いたマクロ、正常に動作しました。
ありがとうございます。

Worksheets("sheet1").Activeではなく.Activateですね。

後、Range(.Cells(j0, x), .Cells(j, x)).SelectとRange(Cells(j0, x), Cells(j, x)).Selectの違いはcelllsの前にドットがあるかどうかですが、この違いは大きいのでしょうか?(重ね重ねスミマセン)

お礼日時:2010/09/20 08:51

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