Excelのマクロ構文を簡潔にして頂けませんでしょうか。 Sub Test() Dim rng1 As Range, rng2 As Range, i As Long
Excelのマクロ構文を簡潔にして頂けませんでしょうか。 Sub Test() Dim rng1 As Range, rng2 As Range, i As Long With Selection If .Areas.Count <> 2 Then Exit Sub Set rng1 = .Areas(1).Resize(10, 4) Set rng2 = .Areas(2).Resize(10, 4) If rng1.Rows.Count <> rng2.Rows.Count Then Exit Sub If rng1.Columns.Count <> rng2.Columns.Count Then Exit Sub End With With rng2 For i = 1 To .Cells.Count If rng1.Cells(i).Interior.ColorIndex = 33 Then If .Cells(i).Interior.ColorIndex = 38 Then .Cells(i).Interior.ColorIndex = 46 End If End If If rng1.Cells(i).Interior.ColorIndex = 33 Then If .Cells(i).Interior.ColorIndex = 4 Then .Cells(i).Interior.ColorIndex = 46 End If End If Next i End With End Sub 以前知恵袋で見たのを便利だと思いコピーしていた物に If rng1.Cells(i).Interior.ColorIndex = 33 Then If .Cells(i).Interior.ColorIndex = 4 Then .Cells(i).Interior.ColorIndex = 46 End If End If 上記を追加してみたら正常に動きましたが、なんか長ったらしくスッキリしません。 If rng1.Cells(i).Interior.ColorIndex = 33 Then If .Cells(i).Interior.ColorIndex = 38 Then .Cells(i).Interior.ColorIndex = 46 End If End If If rng1.Cells(i).Interior.ColorIndex = 33 Then If .Cells(i).Interior.ColorIndex = 4 Then .Cells(i).Interior.ColorIndex = 46 End If End If 上記個所を短くスッキリと出来ないでしょうか。 よろしくお願い致します。Excel2016です。
Visual Basic | Visual Basic・34閲覧・100
ベストアンサー
こんにちは。 For i = 1 To .Cells.Count If rng1.Cells(i).Interior.ColorIndex = 33 Then If .Cells(i).Interior.ColorIndex = 38 or .Cells(i).Interior.ColorIndex = 4 Then .Cells(i).Interior.ColorIndex = 46 End If end if Next i こんなかんじ
質問者からのお礼コメント
こんばんは。 回答頂き有難う御座いました。思い通り動きました。知識が無いもんですからどう仕様も無いです。スッキリしました。 自分で付け足したのは良いのですが、長ったらしくて気持ちが悪かったんです。本当にスッキリしました。有難う御座いました。
お礼日時:5/22 20:28