VBA 実行時エラー1004 について
いつもお世話になります。
作表をしていて、項目に色をつけたいのですが
VBA 実行時エラー1004 Rangeメソッドは失敗しました。Globalオブジェクト
というエラーが
If Range(Cells(5, n - 4)).Interior.Color = RGB(252, 213, 180) Then
のところででます。
If Range("Z5").Interior.Color = RGB(252, 213, 180) Then
とすると、実行できます。
Sub カラー()
Dim n As Long '列番号取得
'最終列取得
n = Cells(5, Columns.Count).End(xlToLeft).Column
MsgBox "最終列は" & n '= 今回は30です。
'セルの色を変える
If Range(Cells(5, n - 4)).Interior.Color = RGB(252, 213, 180) Then
Range(Cells(3, n - 3), Cells(5, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(39, n - 3), Cells(41, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(68, n - 3), Cells(70, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(104, n - 3), Cells(106, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(133, n - 3), Cells(135, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(169, n - 3), Cells(171, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(198, n - 3), Cells(200, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(234, n - 3), Cells(236, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(263, n - 3), Cells(265, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(299, n - 3), Cells(301, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(329, n - 3), Cells(331, n)).Interior.Color = RGB(230, 184, 183)
Range(Cells(365, n - 3), Cells(367, n)).Interior.Color = RGB(230, 184, 183)
Else
Range(Cells(3, n - 3), Cells(5, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(39, n - 3), Cells(41, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(68, n - 3), Cells(70, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(104, n - 3), Cells(106, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(133, n - 3), Cells(135, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(169, n - 3), Cells(171, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(198, n - 3), Cells(200, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(234, n - 3), Cells(236, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(263, n - 3), Cells(265, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(299, n - 3), Cells(301, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(329, n - 3), Cells(331, n)).Interior.Color = RGB(252, 213, 180)
Range(Cells(365, n - 3), Cells(367, n)).Interior.Color = RGB(252, 213, 180)
End If
End Sub
どこが間違っているのか教えていただけないでしょうか?
あと、スマートなコードの書き方もお願いします。
お礼
ご回答いただきまして、ありがとうございます。 原因が判明致しました。 非常にお恥ずかしいのですが、 該当プロシージャを呼び出す順番に問題がございました。 詳細としましては、 別のワークシートに入力した内容(スケジュール表に転記する前の情報)を 今回のvbaよりスケジュール表に転記するようなマクロなのですが、 転記前に「先頭が”祝日”なら・・・」という条件をCallしていた為、 NULLと判断され条件不一致となっておりました。 よって、転記後に同条件のプロシージャをCallすることで、正常に動作するようになりました。 皆様のお手を煩わせてしまい、申し訳ございませんでした。 本当にお恥ずかしいです。。。