• ベストアンサー

エクセルVBAで、ある範囲のデータのある行に対して一行おきに色を付けたい

B3:E3以下の行で一行おきに、C列に文字でも数字でも入力がある場合 その行全体(B:E)を色づけしていきたいのですが、どうすればいいでし ょうか。一般機能の条件付書式でのマクロ記録をとってみましたが、 知識が乏しくどうしたらいいか分かりませんでした。 教えてください。よろしくお願いします。 Sub Macro1() Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$C$3<>""""" Selection.FormatConditions(1).Interior.ColorIndex = 3 End Sub

  • wait4u
  • お礼率45% (619/1365)

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

  • ベストアンサー
  • zap35
  • ベストアンサー率44% (1383/3079)
回答No.3

#01です。そうでしたか、すみません。 条件付き書式をコピーする方法 Sub Macro2()  With ActiveSheet.Range("B3")   .Select   .FormatConditions.Delete   .FormatConditions.Add Type:=xlExpression, Formula1:= _      "=COUNTA($C3)+MOD(ROW(),2)>1"   .FormatConditions(1).Interior.ColorIndex = 3   .Copy  End With  Range("B3:E" & Range("B63556").End(xlUp).Row).Select  Selection.PasteSpecial Paste:=xlPasteFormats  Application.CutCopyMode = False End Sub セルの背景色を直接塗る方法 Sub Macro3() Dim idx, LastR As Long  LastR = Range("B65536").End(xlUp).Row  For idx = 3 To LastR   If Cells(idx, 3) <> "" And (Cells(idx, 3).Row Mod 2) = 1 Then    Range(Cells(idx, "B"), Cells(idx, "E")).Interior.ColorIndex = 3   Else    Range(Cells(idx, "B"), Cells(idx, "E")).Interior.ColorIndex = xlNone   End If  Next idx End Sub どちらもデータの終了判定はB列で行っています

wait4u
質問者

お礼

おつきあいありがとうございました。うまくできました。 いろいろな方法がありおどろきました。 これからが勉強です。

その他の回答 (2)

  • Wendy02
  • ベストアンサー率57% (3570/6232)
回答No.2

こんばんは。 >勉強でマクロでやってみようとしているところです。躓いていてお聞きしたかったのです。 ちょっとおせっかいかもしれませんが、条件付書式は、あまりマクロにすることがありません。それは、それ自身が、動的な変化を持っているからです。 また、うまく行かないのは、「知識が乏しい」のではなく、初級レベルとしては、かなり難問だからです。たったこの程度と思うかもしれませんが、1年ぐらい経っても、てこずるはずです。 Rangeオブジェクトの扱いは、とてもややこしいです。なれるまでには、相当の時間が掛かります。絶対参照と相対参照など、ワークシートの知識も必要になってきます。出来れば、こういうレベルは、後回しにして、通常の書式で、ループを使ってセルに色を塗るマクロをお勧めします。たぶん、マクロの教本でも、このレベルは出てこないと思います。 Sub Macro2() Dim rng As Range Set rng = Range("B3", Range("B65536").End(xlUp))   rng.Resize(, 2).FormatConditions.Delete With rng  .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0" '=1 にすると、もう片方の行になります。  .FormatConditions(1).Interior.ColorIndex = 3    .Offset(, 1).FormatConditions.Add Type:=xlExpression, Formula1:="=C3<>"""""  .Offset(, 1).FormatConditions(1).Interior.ColorIndex = 3 End With Set rng = Nothing End Sub

wait4u
質問者

お礼

貴重なアドバイスありがとうございました。 遠い道のりですが、マイルストーンができた 感じです。

  • zap35
  • ベストアンサー率44% (1383/3079)
回答No.1

マクロが詳しくないなら条件付き書式を使われたらどうですか 条件は「奇数行」でかつ「C列に値がある」ならその行のB:Eに背景色をつけるで良いですか B3を選択して「書式」→「条件付き書式」を選択します 「セルの値が」を「数式が」に変更し、式に以下を貼り付けます。 =COUNTA($C3)+MOD(ROW(),2)>1 「書式」ボタンで「パターン」→「背景色を指定」します。 B3をコピーしてC3:E3に「編集」→「形式を選択して貼り付け」→「書式」で貼り付けます。下方向にも「書式」だけコピー&貼り付けてください

wait4u
質問者

お礼

ありがとうございます。条件付書式で一行おきに色づけは 知っていますが、勉強でマクロでやってみようとしている ところです。躓いていてお聞きしたかったのです。 ありがとうございます。

関連するQ&A

  • 複数の条件付書式

    B列に1~5までに数字がランダムにはいっています。 条件は ・B列が空白ならA列は無色 ・B列が1ならA列は赤 ・B列が2ならA列は青 ・B列が3ならA列は黄色 ・B列が4ならA列は緑 というのをマクロで行ないたいです。 自分で記録して Sub Macro32() Columns("A:A").Select Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=""""" Selection.FormatConditions(1).Interior.Pattern = xlNone Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=1" Selection.FormatConditions(2).Interior.ColorIndex = 38 Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=2" Selection.FormatConditions(3).Interior.ColorIndex = 40 End Sub 4つ目に Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=3" Selection.FormatConditions(4).Interior.ColorIndex = 32 とするとエラーになります。 3つ以上の条件付書式の書き方を教えてください。 あとこの構文のSelectionはwithで省略できますか? よろしくお願いします。

  • excel2000 条件付書式5つ

    A1~J10に100個の数値があり、 行ごとの1位~5位にそれぞれ書式を あたえます。 マクロの記録機能を使って1位~3位を。 その後別で4位・5位を記録し、くっつけて みました。 つけたい書式は 以下のマクロの通りのセルのパターン・フォントの色です。 以下は記録したものをくっつけてつくったマクロです。 動作しません。 Sub 条件付書式5つ() Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=LARGE($A1:$J1,1)" Selection.FormatConditions(1).Font.ColorIndex = 2 Selection.FormatConditions(1).Interior.ColorIndex = 1 Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=LARGE($A1:$J1,2)" Selection.FormatConditions(2).Font.ColorIndex = 2 Selection.FormatConditions(2).Interior.ColorIndex = 16 Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=LARGE($A1:$J1,3)" Selection.FormatConditions(3).Interior.ColorIndex = 15 Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=LARGE($A1:$J1,4)" Selection.FormatConditions(4).Font.ColorIndex = 2 Selection.FormatConditions(4).Interior.ColorIndex = 3 Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=LARGE($A1:$J1,5)" Selection.FormatConditions(5).Font.ColorIndex = xlAutomatic Selection.FormatConditions(5).Interior.ColorIndex = 38 End Sub 初心者のためどうしたらいいのかまったく わかりません。 どうぞお願いします。

  • 列をアルファベットではなく数値で指定するには

    Vbaで条件付き書式を付ける場合、 列をアルファベットではなく数値で指定するにはどうすればいいですか? Sub Macro() Dim i As Long i = 1 Columns(i).Select Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=A1=""aaa""" Selection.FormatConditions(1).Font.ColorIndex = 3 '文字色を赤にする End Sub この場合の、 Formula1:="=A1=""aaa"""のA1を Cells(1,i)みたいにしたいのですが、やり方を教えてください。

  • VBAで条件付書式設定方法

    次の内容の件です Dim wArray As Variant Dim wI As Integer ' セルA3:K3の条件付書式設定 A3の設定:A3=B3は黒,A3<B3は緑,A3>B3は赤,以降同様に wArray = Array(, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L") For wI = 1 To UBound(wArray) - 1    Sheets("Sheet1").Cells(3, wI).Select    Selection.FormatConditions.Delete    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$" & wArray(wI) & "3=$" & wArray(wI + 1) & "3"    With Selection.FormatConditions(1).Font      .Bold = False      .Italic = False      .ColorIndex = xlAutomatic    End With    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$" & wArray(wI) & "3<$" & wArray(wI + 1) & "3"    With Selection.FormatConditions(2).Font      .Bold = False      .Italic = False      .ColorIndex = 10    End With    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$" & wArray(wI) & "3>$" & wArray(wI + 1) & "3"    With Selection.FormatConditions(3).Font      .Bold = False      .Italic = False      .ColorIndex = 3    End With Next wI 実行時にアクティブセルにならないコーディング方法は? また、もっと合理的な、シンプルな方法はないでしょうか? いろいろと、やってみましたが、エラーとか行ズレとなり困っています よろしくご教示ください

  • Excelマクロの記述をもっとわかりやすくしたいのですが、どなたかご教

    Excelマクロの記述をもっとわかりやすくしたいのですが、どなたかご教示お願いします。 Excel2007使用。下記はマクロを記録として作成しました。省略できるものは省略して見やすくしたいのですが、どこを修正していいのか困っています。 範囲は「A2:A1000、B2:B1000」としていますが、「値が入っている範囲」としたい場合はどのように記述したらよいのでしょうか。 マクロ現在勉強中です。よろしくお願いいたします。 Sub CHK() Range("A2:A1000").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF($B$2:$B$1000,A2)>0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.599963377788629 End With Selection.FormatConditions(1).StopIfTrue = False Range("B2:B1000").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF($A$2:$A$1000,B2)>0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.599963377788629 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

  • エクセルのマクロ、VBAに関する質問

    エクセルのマクロ、VBAに関する質問です。 下記コード3行目の"ここヘルプ!"のところを ”「選択範囲左上のセル Selection(1)」を「A1形式」で「行だけ絶対参照 xlAbsRowRelColumn」したものに10足した値” にしたいです。 例:選択範囲がA1:D5なら”A$1+10”   選択範囲がB4:H78なら”B$4+10” もうちょっとで出来る気がするので教えて下さい。 Sub 基準値+1o以上をハッチング() Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="ここヘルプ!" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 52479 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

  • Excel2003 VBAにて条件付き書式のマクロを書きたいのですが、

    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 -------------------------------------------------------------------------------------- この間は、マクロの自動記録で書かれてるので間違ってはいないハズなのですが、、、 お知恵を下さい。 よろしくお願いします。

  • EXCELでマクロの記録を繰り返して

    EXCELでマクロの記録を繰り返して重複した行は分かる範囲でまとめたのですが、もう少しまとめられないかと思っています。 withで、まとめられなくても、順番を入れ替えたらまとまるものとか、逆にまとめようが無いものとか教えて頂けたらと思います。 よろしくお願いします。 With Selection .Phonetics.Visible = False 'フリガナ表示をオフに .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),ISBLANK($L3))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .Pattern = xlSolid .PatternColorIndex = 0 .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),NOT(ISBLANK($D3)))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .TintAndShade = 0 .PatternTintAndShade = 0 .Color = 5296274 End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),ISBLANK($R3))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent5 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False

  • Excel VBA 選択した範囲の1行目に色を付けたい

    VBA初心者です。 セルB4~F15に表が作成されています。 プロシージャを実行して、セルB5~F15にブルー。表1行目のセルB4とF4のみに赤の色を付けるというコードを記述したいのです。 以下のように記述しました。 Sub セルに色()    Range("B4").Select    ActiveCell.CurrentRegion.Select    Selection.Interior.ColorIndex = 8    Selection.Range(Cells(1, 1),Cells(1,5)).Interior.ColorIndex = 3 End Sub Rangeでは連続シートになるため、Unionに変更してみましたが、 Unionはオブジェクトがサポートされていない旨のエラーが出ました。 どのように記述すればよいでしょうか? どうぞよろしくお願いいたします。

  • 単一セルではなく範囲に対して条件付き書式を設定するとき

    条件付き書式について質問です。 A7:M15の範囲を「色範囲」と名前定義しているとして、 その範囲の中で「あ」から始まる文字列の場合、 フォントの色を赤色に設定したいのですが、 1つ1つのセルに対して条件付き書式を設定していくのであれば当然出来きるのはわかります。 Workbooks(book1).Sheets(sheet1).Cells("A7").FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF(A7,""あ*"")" .FormatConditions(1).interior.colorIndex = 6 Workbooks(book1).Sheets(sheet1).Cells("A8").FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF(A8,""あ*"")" .FormatConditions(1).interior.colorIndex = 6 ・・・ ・・・ ・・・ しかし、 範囲全てに対して下記の設定をしてVBAを起動 Workbooks(book1).Sheets(sheet1).Cells(色範囲).FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF(A7,""あ*"")" .FormatConditions(1).interior.colorIndex = 6 エクセル側でA8のセルの条件付き書式をみると、 =COUNTIF(A8,"あ*") と自動で行(列)が変わっているのかわかりません。 要は名前定義をした範囲選択、行番号選択、列番号選択どの場合でも、 左上に表示されたセル座標をCOUNTIFの文字列の引数に設定すればいいのでしょうか。 よろしくお願いいたします。

専門家に質問してみよう