[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『2つのシートのセルに色をつけたい』(まさこ)
Sheets(Array("日本語", "英語")).Selectとして、2つのシートを下記のコードで色をつけたい、これではエラーになり、表示できません。1つずつしかできなませんか。2つとも完成するには、コードをどのようにしたらよいか。お教えくださいませんか。
Sub セル()
Dim i As Integer, j As Integer
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False Application.Calculation = xlCalculationManual
Sheets(Array("日本語", "英語")).Select
For i = 2 To LastRow
If Cells(i, "Q") = "欠" Or Cells(i, "R") = "欠" Or Cells(i, "S") = "欠" Then
Cells(i, "V") = "欠" End If
If Cells(i, "Q") = "欠" Then
Cells(i, "Q").Interior.ColorIndex = 46 '46は薄いオレンジ End If If Cells(i, "R") = "欠" Then Cells(i, "R").Interior.ColorIndex = 46 '46は薄いオレンジ End If If Cells(i, "S") = "欠" Then Cells(i, "S").Interior.ColorIndex = 46 '46は薄いオレンジ End If If Cells(i, "V") = "欠" Then Cells(i, "V").Interior.ColorIndex = 46 '46は薄いオレンジ End If
Next i Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True
End Sub
< 使用 Excel:Excel2013、使用 OS:Windows10 >
色を塗る、文字を入力の場合は、複数のシートに対して同時にできますが、 取得に関しては、1つだけです。
だから、あえて提示された個度を使うとすると、
↓シートを指定 If Sheets("日本語").Cells(i, "Q") = "欠" Then 'Cells(i, "Q").Interior.ColorIndex = 46 '46は薄いオレンジ ↓一々セルを選択して、対象をSeectionにしないとできない。 Cells(i, "Q").Select Selection.Interior.ColorIndex = 46 End If
普通は、各シートに対して1回づつ処理すると思いますよ。 複数のシートに対して1度で同じ処理をしたいとか考えない方が良いと思うけどね。 たぶん、上のようなコードは、ほとんどの人が書かないと思います。
(BJ) 2019/01/22(火) 00:16
あと、人に読ませようと思うなら、インデントなどをそろえるなり、 意味のわからない空白行を作らないようにして、読みやすいコードにしましょう。 (BJ) 2019/01/22(火) 00:22
Sub セル() Dim i As Long Dim ws As Worksheet Dim LastRow As Long Dim col As Variant
'シートのループ For Each ws In Sheets(Array("日本語", "英語")) With ws If .ProtectContents = True Then 'シートの保護状態確認 MsgBox ws.Name & "シートが保護されています。解除してから実行してください" Else Application.ScreenUpdating = False Application.Calculation = xlCalculationManual
'シートごとの最終行取得 LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
'行のループ For i = 2 To LastRow 'Q,R,S列のどれか一つでも欠なら、V列も欠 'でもこれはシートに計算式のほうがいいんじゃない? If .Cells(i, "Q") = "欠" Or .Cells(i, "R") = "欠" Or .Cells(i, "S") = "欠" Then .Cells(i, "V") = "欠" End If
'Q,R,S,V列に対して、同じ処理をする場合、For Each構文 .Range("Q:S,V:V").Interior.ColorIndex = xlNone For Each col In Array("Q", "R", "S", "V") If .Cells(i, col) = "欠" Then .Cells(i, col).Interior.ColorIndex = 46 '46は薄いオレンジ Next col Next i
Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End If End With Next ws End Sub
こんなんでどうでしょう? (稲葉) 2019/01/22(火) 08:50
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.