質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

Q&A

解決済

3回答

5287閲覧

VBA 条件付き書式の一括設定

chihuahua_house

総合スコア23

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

0グッド

0クリップ

投稿2019/02/05 13:59

条件付き書式のループ処理について質問させて下さい。

以下ソースコード
Sub test()
Dim a As Integer, b As Integer
With Sheets("プロパティ")
a = .Range("C2").Value
b = .Range("D2").Value

With Range("D11") .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($D11>=" & a & ",$D11<" & b & ")" .FormatConditions(.FormatConditions.Count).SetFirstPriority .FormatConditions(1).Interior.Color = 65535 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D11>=" & b .FormatConditions(.FormatConditions.Count).SetFirstPriority .FormatConditions(1).Font.Bold = True .FormatConditions(1).Interior.Color = 255 With Range("E11") .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($E11>=" & a & ",$E11<" & b & ")" .FormatConditions(.FormatConditions.Count).SetFirstPriority .FormatConditions(1).Interior.Color = 65535 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D11>=" & b .FormatConditions(.FormatConditions.Count).SetFirstPriority .FormatConditions(1).Font.Bold = True .FormatConditions(1).Interior.Color = 255

End sub

上記コードでD11,E11セルに対して条件付き書式の設定を行う事が出きたのですが
AG11セルまで同様の条件付き書式を設定したいのですが
1つ1つ設定するのはコード量が多くなるため出来るだけ避けたく
for文で回せないか考えたのですか、Cellsによる処理の記載方法が調べても理解が出来ませんでした。

詳しい方、ご教授お願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

解決している問題に解答をつけるのもなんですが、対象のセルに設定する条件が同じ(要はC3セル以上かC4セル以上)ならば、ループにせずとも、セル範囲に対して条件式中の評価対象セルは絶対参照をしないように($D11の記述をD11へ)した条件付き書式をつければ済むのでは。

VBA

1With Range("D11:AG11") 2 .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(D11>=" & a & ",D11<" & b & ")" 3 .FormatConditions(.FormatConditions.Count).SetFirstPriority 4 .FormatConditions(1).Interior.Color = 65535 5 .FormatConditions.Add Type:=xlExpression, Formula1:="=D11>=" & b 6 .FormatConditions(.FormatConditions.Count).SetFirstPriority 7 .FormatConditions(1).Font.Bold = True 8 .FormatConditions(1).Interior.Color = 255 9End With

あとで見たときに分かりにくくなるのであまり好きな使い方ではありませんが。

投稿2019/02/05 15:39

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

書式設定の処理も実装してみました。

VBA

1Sub test2() 2 3 Dim a As Integer, b As Integer 4 Dim row As Long 5 Dim col As Long 6 Dim formula1 As String 7 Dim formula2 As String 8 9 With Worksheets("プロパティ") 10 a = .Cells(2, 3).Value 11 b = .Cells(2, 4).Value 12 End With 13 14 ' 1('A')~33('AG')までループ 15 row = 11 16 For col = 1 To 33 17 18 With Range(Cells(row, col), Cells(row, col)) 19 20 formula1 = "=AND(R" & row & "C" & col & " >= " & a & ",R" & row & "C" & col & " < " & b & ")" 21 formula2 = "=R" & row & "C" & col & " >= " & b 22 23 .FormatConditions.Add Type:=xlExpression, formula1:=formula1 24 .FormatConditions(.FormatConditions.Count).SetFirstPriority 25 .FormatConditions(1).Interior.Color = 65535 26 27 .FormatConditions.Add Type:=xlExpression, formula1:=formula2 28 .FormatConditions(.FormatConditions.Count).SetFirstPriority 29 .FormatConditions(1).Font.Bold = True 30 .FormatConditions(1).Interior.Color = 255 31 32 End With 33 Next 34 35End Sub

投稿2019/02/05 14:56

mika33532

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

chihuahua_house

2019/02/05 15:16

ありがとうございます。 無事処理ができました。
guest

0

こんな感じでどうでしょう。

Sub test()

Dim row As Long Dim col As Long row = 11 For col = 1 To 20 With Range(Cells(row, col), Cells(row, col)) .Value = col End With Next

End Sub

投稿2019/02/05 14:21

mika33532

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問