[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『指定したセルに含まれない文字を色付けするマクロ』(yyyfg)
例えばA1に”文字列”がありB1に記載してある文字列とを比較してA1に含まれない文字を判別しB1に記載のA1に含まれない文字のみに色付けするといったようなマクロを望んでいるのですが可能でしょうか?
要は隣接したセル同士を比較して異なる文字があれば、色を付ける動作を望んでおります><;
< 使用 Excel:Excel2007、使用 OS:Windows7 >
力技で美しくないですが。
Sub Sample() Dim dic As Object Dim x As Long Dim pos1 As Range Dim pos2 As Range
Set dic = CreateObject("Scripting.Dictionary") Set pos1 = Range("A1") Set pos2 = pos1.Offset(, 1)
For x = 1 To Len(pos1.Value) dic(Mid(pos1.Value, x, 1)) = True Next
pos2.Font.ColorIndex = xlAutomatic
For x = 1 To Len(pos2.Value) If Not dic.exists(Mid(pos2, x, 1)) Then pos2.Characters(x, 1).Font.Color = vbRed Next
End Sub
(β) 2016/11/29(火) 01:07
↑ やっぱり、おバカなコードでした。以下で充分ですね。
Sub Sample2()
Dim x As Long
Dim pos1 As Range
Dim pos2 As Range
Set pos1 = Range("A1")
Set pos2 = pos1.Offset(, 1)
pos2.Font.ColorIndex = xlAutomatic
For x = 1 To Len(pos2.Value)
If InStr(pos1.Value, Mid(pos2.Value, x, 1)) = 0 Then pos2.Characters(x, 1).Font.Color = vbRed
Next
End Sub
(β) 2016/11/29(火) 01:16
↑は A1 と B1 の比較ですが、やりたいことが、A列とB列に文字列があって、同じ行の A列のセルとB列のセルの比較を
一括処理したいということであれば、Sample2 をベースにして繰り返しを加味。
Sub Sample3()
Dim x As Long
Dim pos1 As Range
Dim pos2 As Range
With Range("B1", Range("B" & Rows.Count).End(xlUp))
.Font.ColorIndex = xlAutomatic
For Each pos2 In .Cells
Set pos1 = pos2.Offset(, -1)
For x = 1 To Len(pos2.Value)
If InStr(pos1.Value, Mid(pos2.Value, x, 1)) = 0 Then pos2.Characters(x, 1).Font.Color = vbRed
Next
Next
End With
End Sub
(β) 2016/11/29(火) 11:14
なぜ、私の最初のレスのみを残し、質問文と、そのあとのコード、 ならびに、そのあとの質問者さんからのレスを削除されたのかわかりませんけど、 私のレスのみ、復元しておきました。
誰が、何の目的で、こういった編集を行うのでしょうかね? (β) 2016/11/30(水) 21:39
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.