下記のコードで①から③までは実行するのですが、
④⑤が実行致しません。
わかる方おしえてくれませんでしょうか
フォントを追加したせいなのかもしれません。
よろしくお願いいたします。
①E列 行が 偶数 日付 ならば フォント 青 でなければ 黒
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If Not Intersect(Target, Range("E:E")) Is Nothing Then
If .Row Mod 2 = 0 Then
If IsDate(.Value) Then
Cells(.Row, 5).Font.ColorIndex = 5
Else
Cells(.Row, 5).Font.ColorIndex = xlAutomatic
End If
End If
② E列 行が 奇数 日付 ならば フォント 赤 でなければ 色なし
偶数行のフォントは 青
If Not Intersect(Target, Range("E:E")) Is Nothing Then
If .Row Mod 2 = 1 Then
If IsDate(.Value) Then
Cells(.Row, 5).Font.ColorIndex = 3
Cells(.Row - 1, 5).Font.ColorIndex = 3
Else
Cells(.Row, 5).Font.ColorIndex = xlNone
Cells(.Row - 1, 5).Font.ColorIndex = 5
End If
End If
③ E列 行が 奇数 日付 ならば 背景色 奇数行 偶数行 6 でなければ 色なし
偶数行のフォントは 青
If Not Intersect(Target, Range("E:E")) Is Nothing Then
If .Row Mod 2 = 1 Then
If IsDate(.Value) Then
Cells(.Row, 5).Interior.ColorIndex = 6
Cells(.Row - 1, 5).Interior.ColorIndex = 6
Cells(.Row, 3).Interior.ColorIndex = 6
Cells(.Row - 1, 3).Interior.ColorIndex = 6
Else
Cells(.Row, 5).Interior.ColorIndex = xlNone
Cells(.Row - 1, 5).Interior.ColorIndex = xlNone
Cells(.Row, 3).Interior.ColorIndex = xlNone
Cells(.Row - 1, 3).Interior.ColorIndex = xlNone
End If
End If
④ F列 行が 奇数 日付 ならば 背景色 奇数行 偶数行 3 でなければ 色なし
ElseIf Not Intersect(Target, Range("f:f")) Is Nothing Then
If .Row Mod 2 = 1 Then
If IsDate(.Value) Then
Cells(.Row, 6).Interior.ColorIndex = 3
Cells(.Row - 1, 6).Interior.ColorIndex = 3
Else
Cells(.Row, 6).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 6).Interior.ColorIndex = xlColorIndexNone
End If
End If
ElseIf Not Intersect(Target, Range("h:h")) Is Nothing Then
If .Row Mod 2 = 1 Then
If IsDate(.Value) Then
Cells(.Row, 8).Interior.ColorIndex = 7
Cells(.Row - 1, 8).Interior.ColorIndex = 7
Cells(.Row, 3).Interior.ColorIndex = 7
Cells(.Row - 1, 3).Interior.ColorIndex = 7
Else
Cells(.Row, 8).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 8).Interior.ColorIndex = xlColorIndexNone
Cells(.Row, 3).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 3).Interior.ColorIndex = xlColorIndexNone
End If
End If
⑤
ElseIf Not Intersect(Target, Range("k:k,L:L")) Is Nothing Then
If IsDate(Cells(.Row, 11).Text) And IsDate(Cells(.Row, 12).Text) Then
If Cells(.Row, 11).Value >= Cells(.Row, 12).Value Then
Cells(.Row, 12).Interior.ColorIndex = 3
Else
Cells(.Row, 12).Interior.ColorIndex = xlColorIndexNone
End If
Else
Cells(.Row, 12).Interior.ColorIndex = xlColorIndexNone
End If
End If
End If
End If
End With
End Sub
No.7ベストアンサー
- 回答日時:
No.4です。
気まぐれに作ってみた物ですが動作検証が正しいのかは良くわかりませんでした。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim st As String
With Target
st = Left(.Address(0, 0), 1)
Select Case st
Case "E"
If .Row Mod 2 = 0 And IsDate(.Value) Then
Cells(.Row, 5).Font.ColorIndex = 5
Else
Cells(.Row, 5).Font.ColorIndex = xlAutomatic
End If
If .Row Mod 2 = 1 And IsDate(.Value) Then
Cells(.Row, 5).Font.ColorIndex = 3
Cells(.Row - 1, 5).Font.ColorIndex = 3
Cells(.Row, 5).Interior.ColorIndex = 6
Cells(.Row - 1, 5).Interior.ColorIndex = 6
Cells(.Row, 3).Interior.ColorIndex = 6
Cells(.Row - 1, 3).Interior.ColorIndex = 6
Else
Cells(.Row, 5).Font.ColorIndex = xlAutomatic '何故かxlNoneではダメだった。古いからかも。
Cells(.Row - 1, 5).Font.ColorIndex = 5
Cells(.Row, 5).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 5).Interior.ColorIndex = xlColorIndexNone
Cells(.Row, 3).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 3).Interior.ColorIndex = xlColorIndexNone
End If
Case "F"
If .Row Mod 2 = 1 And IsDate(.Value) Then
Cells(.Row, 6).Interior.ColorIndex = 3
Cells(.Row - 1, 6).Interior.ColorIndex = 3
Else
Cells(.Row, 6).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 6).Interior.ColorIndex = xlColorIndexNone
End If
Case "H"
If .Row Mod 2 = 1 And IsDate(.Value) Then
Cells(.Row, 8).Interior.ColorIndex = 7
Cells(.Row - 1, 8).Interior.ColorIndex = 7
Cells(.Row, 3).Interior.ColorIndex = 7
Cells(.Row - 1, 3).Interior.ColorIndex = 7
Else
Cells(.Row, 8).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 8).Interior.ColorIndex = xlColorIndexNone
Cells(.Row, 3).Interior.ColorIndex = xlColorIndexNone
Cells(.Row - 1, 3).Interior.ColorIndex = xlColorIndexNone
End If
Case "K", "L"
If IsDate(Cells(.Row, 11).Text) And IsDate(Cells(.Row, 12).Text) Then
Cells(.Row, 12).Interior.ColorIndex = IIf(Cells(.Row, 11).Value >= Cells(.Row, 12).Value, 3, xlColorIndexNone)
Else
Cells(.Row, 12).Interior.ColorIndex = xlColorIndexNone
End If
End Select
End With
End Sub
たまには検証してみないとVBA忘れそうですし。
No.5
- 回答日時:
何気に見ていただけですが、②と③って条件同じなので一緒にしても良いように思えますが何か不都合でも?
それとTargetが1つの列しか当てはまらない部分はSelect Case でも良いのでは?って思えました。
No.4
- 回答日時:
①、②、③の
If Not Intersect(Target, Range("E:E")) Is Nothing Then
を①、②、③内で完結させて
④、⑤のElseIf⇒if に変更する。
[⑤の最後のEnd Ifの個数は見直して下さい]
No.3
- 回答日時:
①If Not Intersect(Target, Range("E:E")) Is Nothing Then
②If Not Intersect(Target, Range("E:E")) Is Nothing Then
③If Not Intersect(Target, Range("E:E")) Is Nothing Then
④ElseIf Not Intersect(Target, Range("f:f")) Is Nothing Then
④ElseIf Not Intersect(Target, Range("h:h")) Is Nothing Then
⑤ElseIf Not Intersect(Target, Range("k:k,L:L")) Is Nothing Then
③が成立してしまうと、④、⑤には入ってきません。
また、③非成立でも、④のどちらかが成立してしまうと、⑤には入ってきません。
No.2
- 回答日時:
こんにちは
インデントを利用するなどで、条件処理の範囲をきちんと把握できるようにしておけば、間違えは少なくなるものと思います。
ご提示のコードを簡略化すると、
If Not Intersect(Target, Range("E:E")) Is Nothing
'②
If Not Intersect(Target, Range("E:E")) Is Nothing
'③
If Not Intersect(Target, Range("E:E")) Is Nothing
'④
ElseIf Not Intersect(Target, Range("f:f")) Is Nothing
'⑤
ElseIf Not Intersect(Target, Range("k:k,L:L")) Is Nothing
End If
End If
End If
という構成になっています。
④⑤の処理は、最初からの条件分岐でTargetがE列の場合にしか実行されませんので、当然ながら条件には合致せず、処理内容が実行されることがないものと考えられます。
なさりたい内容に合わせて、コード化することが必要かと思われます。
No.1
- 回答日時:
以下のような構文となっています。
簡単に言えば
④が③直後のIfのElseIfになっていますが、最初に同条件のTrueの部分に存在するため、Else(=Fales)とはなり得ないのです。
④⑤に関しては最初のIfのElseIfとしないといけないのではないでしょうか。
If Not Intersect(Target, Range("E:E")) Is Nothing Then
If .Row Mod 2 = 0 Then
・・・・
End If
② E列 行が 奇数 日付 ならば フォント 赤 でなければ 色なし偶数行のフォントは 青
If Not Intersect(Target, Range("E:E")) Is Nothing Then
③ E列 行が 奇数 日付 ならば 背景色 奇数行 偶数行 6 でなければ 色なし偶数行のフォントは 青
If Not Intersect(Target, Range("E:E")) Is Nothing Then
If .Row Mod 2 = 1 Then
・・・・・
End If
④ F列 行が 奇数 日付 ならば 背景色 奇数行 偶数行 3 でなければ 色なし
ElseIf Not Intersect(Target, Range("f:f")) Is Nothing Then
If .Row Mod 2 = 1 Then
・・・・・
End If
ElseIf Not Intersect(Target, Range("h:h")) Is Nothing Then
If .Row Mod 2 = 1 Then
・・・・・
End If
⑤
ElseIf Not Intersect(Target, Range("k:k,L:L")) Is Nothing Then
・・・
End If
End If
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Visual Basic(VBA) VBAで実行時エラー'424' オブジェクトが必要ですと出る 2 2022/10/07 09:25
- Visual Basic(VBA) VBAコードが作動せず、どこに問題があるのか教えて下さい。 3 2023/06/13 13:20
- Visual Basic(VBA) エクセルVBA ダブルクリックしたら色反転を指定したセルのみにしたい 2 2022/04/06 12:52
- Excel(エクセル) マクロで列を加えたら上手くいかなくなりました。 2 2022/05/23 17:59
- Excel(エクセル) マクロで最終行から上に検索を逆にしたい 1 2022/05/17 18:27
- Visual Basic(VBA) VBA 別ブックからの転記の高速化について VBA 別ブックからの転記の高速化についてご教授下さい。 19 2022/07/26 13:07
- Visual Basic(VBA) vbaを早くしたい 5 2022/09/09 10:58
- Excel(エクセル) R列の1111/11/11以外、且つQ列の×の条件で該当行のAからAE列までオレンジに塗りつぶす 2 2022/07/02 10:18
- Visual Basic(VBA) ExcelVBAでDo Until loopのネスト、IF文を使って一致する物と一致しない物としたい 11 2022/12/24 17:46
- Visual Basic(VBA) いつもお世話になっております、VBAで教えて頂きたいのですが 2 2022/05/05 22:20
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
outlook 文字を揃えたい。tab...
-
マイクロ(μ)の文字を半角で出...
-
教えてください。
-
Excel:一部のフォントでセルの...
-
エクセルVBAが中国のPCでは動か...
-
C# Font GdiCharSet
-
TeraPadとメモ帳
-
CommonDialogでの、キャンセル...
-
コマンドプロンプト(cmd.exe)を...
-
font-family の記載方法
-
WORD VBAでハイパーリ...
-
インストールされていないフォ...
-
PDFファイルにフォントを埋め込...
-
ホームページをTeraPadで日本語...
-
HPで使って文字化けしないフォ...
-
似ているフォント(Win⇔Mac)
-
Eclipseで文字がずれます。
-
WORD全角入力しても半角に...
-
縦書きテキストボックスの表示"...
-
MsgBoxについて
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
outlook 文字を揃えたい。tab...
-
マイクロ(μ)の文字を半角で出...
-
教えてください。
-
MsgBoxについて
-
「MS Pゴシック」と同じ幅の...
-
似ているフォント(Win⇔Mac)
-
英語版OSでの遊明朝、游ゴシック
-
縦書きテキストボックスの表示"...
-
font-family の記載方法
-
C# Font GdiCharSet
-
cssで英語と日本のフォント...
-
txtは Wordでの明朝がゴシック...
-
メールで、榊の文字を木辺に神...
-
WORD VBAでハイパーリ...
-
Webページ内での表示について
-
フォントファイル(.ttf , .otf)...
-
逆向き/(スラッシュ)の入力方法
-
パソコンによってフォントが違う
-
printf("文字列\\n")が円マーク...
-
ワードでゴシックの文字を太く...
おすすめ情報
申し訳ございません。
どうしたらいいのか。
わかりません。
④⑤の処理は、最初からの条件分岐でTargetがE列の場合にしか実行されませんので
ここまではわかりました。
となると、④⑤にたどりつくには申し訳ございません
おしえてくれませんでしょうか
条件分岐で検索 申し訳ございません
おしえてくれませんでしょうか
①、②、③の
If Not Intersect(Target, Range("E:E")) Is Nothing Then
を①、②、③内で完結させて
というのは一つにまとめるということですか