エクセルで条件付き書式設定を使ってセルの背景を赤にする方法

このQ&Aのポイント
  • エクセルで条件付き書式設定を使って、マウスで選択した範囲に80点以上のセルの背景色を赤にする方法について教えてください。
  • マクロを使って条件付き書式設定を適用し、セルの背景を赤にする方法を知りたいです。範囲の取得はマクロ実行時に手動で指定したいのですが、具体的にはどのようなコードを書けば良いのでしょうか?
  • エクセルのマクロを作成して、マウスで選択した範囲に80点以上のセルの背景を赤にする方法について教えてください。具体的なコードの書き方や注意点なども教えていただけると嬉しいです。
回答を見る
  • ベストアンサー

エクセルについてです。マウスで選択した範囲に、条件付き書式設定で80点

エクセルについてです。マウスで選択した範囲に、条件付き書式設定で80点以上のセルの背景を赤にする、というマクロを作成したいと思います。選択する範囲は毎回異なります。以下のマクロはマクロの記録で作成したものです。このRangeの部分をマウスで選択した範囲に変更したいのですが、どうしたら良いのでしょうか?範囲の取得はマクロを実行して、途中で「マウスで範囲を指定してください」というメッセージを表示させてからにしたいのですが。 Sub 背景を赤() Range("B13:E19").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=80" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Color = -16383844 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 13551615 .TintAndShade = 0 End With End Sub

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

  • ベストアンサー
  • xls88
  • ベストアンサー率56% (669/1189)
回答No.1

Range("B13:E19").Select を Application.InputBox("セル範囲を選択", Type:=8).Select に書き換えてください。 実行すると、InputBoxが表示されます。 セル範囲をマウスでドラッグして選択すれば選択範囲のアドレスが入力されます。 「OK」すればVBAに戻って処理が継続されます。 直接セルアドレスを入力しても可です。 実は奥が深いInputBox http://www.officetanaka.net/excel/vba/tips/tips37.htm

yasucosmos
質問者

お礼

なぜマクロが止まったか、いろいろと試してみて分かりました。実行する前に同じ条件付き書式が設定してあると、先ほどのエラーが生じました。全く初めての範囲でしたら問題なく実行できました。ご回答、有り難うございました。参考になりました。

yasucosmos
質問者

補足

Sub Macro1() ' Application.InputBox("セル範囲を選択", Type:=8).Select Range("B13:E19").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=101" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Color = -16383844 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 13551615 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False End Sub これを実行すると .Color = -16383844 ここでマクロが止まってしまいます。なぜか分かりません。Rangeの時は実行します。マクロの記録で作成したのですから当然ですが。

その他の回答 (3)

  • xls88
  • ベストアンサー率56% (669/1189)
回答No.4

With文で括って下記のように纏めることもできます。 Selectの必要が無くなります。 With Application.InputBox("セル範囲を選択", Type:=8) .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=101" .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1).Font .Color = -16383844 .TintAndShade = 0 End With With .FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 13551615 .TintAndShade = 0 End With End With

yasucosmos
質問者

補足

いくつか方法があるのですね。参考になりました。ご回答有り難うございました。

  • xls88
  • ベストアンサー率56% (669/1189)
回答No.3

>回答番号:No.1 この回答への補足 エラーになるのは良く解りませんが 回答文を良く見てください。 捕捉で提示されたコードでは Application.InputBox("セル範囲を選択", Type:=8).Select と Range("B13:E19").Select で重複しています。 一部再掲します。 ----------------------------------------------------- Range("B13:E19").Select を Application.InputBox("セル範囲を選択", Type:=8).Select に書き換えてください。 -----------------------------------------------------

yasucosmos
質問者

補足

よく見るとそうでしたね。分かりました。現在マクロは思ったように実行されています。アドバイスを有り難うございました。

  • kmetu
  • ベストアンサー率41% (562/1346)
回答No.2

Selection.が選択したセル範囲になりますから Range("B13:E19").Selectを削除するだけで 現在選択されている部分に対しての操作になります。

関連するQ&A

  • エクセルのマクロ、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

  • 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 選択範囲の中で、更に一番上の行を指定したい!

    タイトルの通りです。 選択範囲に罫線を引くマクロを作成しました。 外に太い枠線、中は点線を引き、選択範囲の一番上の行を灰色の塗りつぶしにしたいのです。 しかし、罫線はうまくいきましたが、一番上の行の指定がうまくできません。 Sub 罫線をひく() ' With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With ActiveWindow.SmallScroll Down:=12  ←ここを変えたいのですが、どうすればよいかわかりません。 With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.499984740745262 .PatternTintAndShade = 0 End With End Sub どうすればよいか、もしご存知の方がいらっしゃいましたら、ご指導ください。 よろしくお願いたします。

  • Excel2010のバグ?(条件つき書式)

    test1は、A~X列に数字と条件付き書式をセットするマクロですが、途中のセルから色化けします。 原因がお分かりの方はご教授ください。どうもExcel2010のバグのように思われます。 バグならMicrosoftのどこに報告あるいは問い合わせれば良いでしょうか。 Sub test1() Dim aRow As Long Dim aCol As Long With ActiveSheet For aRow = 1 To 3 For aCol = 1 To 25 .Cells(aRow, aCol).Value = aCol Call SetFormatConditionOfColor(aRow, aCol, "=1") Next Next End With End Sub Public Sub SetFormatConditionOfColor(ByVal aRow As Long, ByVal aCol As Long, ByVal aFormula As String) With ActiveSheet.Cells(aRow, aCol) .FormatConditions.Delete '一致するセル .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:=aFormula With .FormatConditions(1) .Font.Color = 0 .Interior.Color = 13434879 End With '一致しないセル .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _ Formula1:=aFormula With .FormatConditions(2) .Font.Color = 16777215 .Interior.Color = 16767843 End With End With End Sub OS: MS WindowsXP Professional Service Pack3 Excel: Microsoft Office Professional Plus 2010 14.0.5128.5000(32ビット)

  • 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

  • Excel2013,macroのcopy

    Excel2013でマクロ実行するのですが、最後のstepの「条件付き書式」のcopyが上手く行きません。どなたかご教授お願いします。 A~J列x約4000行(1行~5行まではTytle行)のsheetです。 A列は日付で、過去からの日付となって居ます。 このsheetを見易くする為にA6行に 「条件付き書式」 =mod(month(A6),2)=0 を設定し、下記のmacro式でA6~J4000にCOPYするのですが、結果は上手く行きません。 ※1:偶数月行のA列のみ正確に書式設定されるが、全列(A~J)にならない。 ※2:奇数月のところどころ(列)に書式が設定される。 の不具合が生じます。 考えられる原因は何でしょうか?、ご教授願えませんでしょうか?。 与えたmacro Range("A6").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=MOD(MONTH(A6),2)=0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 11200714 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = True Selection.Copy Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Range("A6").Select End Sub

  • 条件付書式をVBAでセルに設定するときのルール

    図のような列があります。 C列にこだわっているわけではないのですが、その左側にある列で計算をしているという意味で、A、B列はあけただけです。 で、IF文で計算してある条件に合致するとC列で「TRUE」を表示し、合致していない場合はそのときの計算結果を表示させています(IF文的に正しくないので、計算したままの値が計算結果として表示される)。 この結果の見せ方として、これまではC列に「条件付書式」を手動で設定し、文字がTRUEのとき字を青くする表示にしていたのですが、同様の処理を行うExcel Bookファイルが多いため、VBAで条件書式を設定しようとし、次のような記述を実行しました。 すると、図のような結果になりました。 ---------------------------------------------- Sub TestSample() Dim i As Long i = 20 '実際には最終行はBookによって異なるので、 ’行末検索させせていますが、とりあえずここでは20としておきます。 With ActiveSheet Range(Cells(2, 3), Cells(i, 3)).Select With Selection     FormatConditions.Add Type:=xlTextString, String:="TRUE", _     TextOperator:=xlContains     .FormatConditions(1).Font.Color = RGB(0, 50, 255) ’ーーA     .FormatConditions(1).Interior.TintAndShade = 0   ’ーーB End With End With End Sub ---------------------------------------------- 質問が2つあります。 1. 背景がいわゆる「色なし」ではなく白くなっていますが、背景「色なし」かつ「罫線でかこむ」には、どのような記述をすればよいでしょうか 2. FormatConditions.Add Type:~~~~ につづく別の行(Aのところ)に「FormatConditions.」と記述すると実行できません。 マクロで条件書式の自動記録をやって参考にしたところ、添値のような(1)が記録されることに気が付き、それを真似して FormatConditions(1). としたところ、動作しました。 最初だけFormatConditionsで、なぜAのところはFormatConditions(1).となるのか、またAとBはどちらも「FormatConditions(1).」でダブっているのに処理上エラーを返さないのか、理屈がわかりません。 (1)がつく意味と、AとBはどちらも(1)で(ダブっても)動作するのか(逆にいえば、なぜ最初のFormatConditionsだけ、番号がついていないのか、たぶんデフォルトで(0)の意味?) について、このへんの仕組みの解説をよろしくお願いします。

  • 保護されているシートでマクロ実行するとエラー

    Excel2010で勤務表を作っています。 A列にとある文字列(承認)と入力すると、その行が保護されるマクロを、下記URLからコピペして使わせて頂いてます。 http://questionbox.jp.msn.com/qa3277541.html 勤務表なので、土日祝日は網掛けになるよう条件付き書式を使っています。 休暇取得した場合は網掛けを付けて、休日出勤した場合には網掛けなしにしたり出来るようマクロをマクロの自動記録で作りました。 ところが、どこかの行が保護されている状態で、セルの網掛けを変更するマクロを実行すると 「実行時エラー'1004'アプリケーション定義またはオブジェクトの定義エラーです」と出てしまいます。 保護されている行ではなく、まだ保護はされていない行に実行しています。 エラーが出ているのは網掛けマクロから出ています。 以下、今エクセルファイルにあるマクロの構文になります。 網掛けマクロは全部で4つ作りました。 1)休日出勤した際に条件付き書式をクリアして網掛けなしにするマクロです。 Sub 休日出勤() ' 条件書式クリア Selection.FormatConditions.Delete End Sub ※エラーになっている部分です。 2)平日休んだ日に網掛けをするマクロです。 Sub 休日() ' 網掛け With Selection.Interior .ColorIndex = 0 .Pattern = xlGray16 .PatternColorIndex = xlAutomatic End With End Sub 3)2)のマクロで休日にしたけど、やっぱり出勤したという時に、1)だと網掛けなしにならなかったので、網掛けなしにするマクロを作りました。 Sub網掛けなし() ' 網掛けなし With Selection.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub 4)ごちゃごちゃいじってしまって最初の状態に戻したいと思ったので条件付き書式を再設定するマクロを作りました。が、2)の休日マクロを実行したセルは元に戻らないので仕方なく3)の網掛けなしマクロを実行しなければなりません。 Sub 書式クリア() ' 条件書式再設定 Range("A6:K36").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=WEEKDAY($B6,2)>=6" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Pattern = xlGray16 .PatternColorIndex = xlAutomatic .ColorIndex = xlAutomatic End With Selection.FormatConditions(1).StopIfTrue = False Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=OR(WEEKDAY($B6)=1,COUNTIF(祝日,$B6))" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Pattern = xlGray16 .PatternColorIndex = xlAutomatic .ColorIndex = xlAutomatic End With Selection.FormatConditions(1).StopIfTrue = False End Sub ※この中のSelection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=WEEKDAY($B6,2)>=6"の部分がエラーと出ています。 ※1)から3)は網掛けしたいところ、網掛けなしにしたいところを範囲選択してから実行しなければなりません。 その他、保護を解除する際にパスワード認証が欲しかったので、「保護解除」ボタンを押すためにパスワード認証させるマクロもあります。 これは特に問題なく動いています。 5)パスワード認証つき保護解除マクロ Sub password() Dim pw As Long pw = Application.InputBox( _ prompt:="パスワード入力", Type:=1) If pw <> "123" Then MsgBox "パスワードが違います" Exit Sub Else MsgBox "保護解除しました" ActiveSheet.Unprotect End If End Sub 6)行ごとに保護するマクロです。 Private Sub Worksheet_Change(ByVal Target As Range) Dim r, rng As Range Set rng = Intersect(Target, Columns(1)) If Not rng Is Nothing Then If ActiveSheet.ProtectContents = True Then ActiveSheet.Unprotect End If For Each r In rng If r.Value = "承認" Then r.EntireRow.Locked = True Else r.EntireRow.Locked = False End If Next r ActiveSheet.Protect DrawingObjects:=True, Contents:=True End If End Sub マクロに関してはド素人で、自動記録かWebで調べて見つけたマクロをちょっと加工して使う程度です。 どうか知恵をお貸しください。よろしくお願いします。

  • エクセルのマクロで範囲指定をセルに番号を入れて、範囲指定したセルの字の

    エクセルのマクロで範囲指定をセルに番号を入れて、範囲指定したセルの字の色を白にして消したいのですが、番号を入れるマクロがわかりません。マクロに記録でA2からB12までのセル内の字の色を白 にする方法は Sub 字を消す() ' ' 字を消す Macro ' ' Range("A2:B11").Select With Selection.Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With End SubでできるのですがD3に開始番号、E3に終了番号を指定する方法がわかりません。ご教授お願いします

  • 複数の条件付書式

    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で省略できますか? よろしくお願いします。

専門家に質問してみよう