2016-10-28 6 views
1

特定の特殊文字を持つセルを(例:!、。=] \ ')として識別する方法を理解する必要があります。列には数字(0-9)、文字(az)キャップ(AZ)として表示し、色としてマークします。特殊文字の識別VBA

例:enter image description here

私はコードとして自動的にこれをしたいです。

お時間をいただきありがとうございます。

+2

Regex - > http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops – DejaVuSansMono

+1

これは文字と数字、セルにも ' - 'をつけることができますか? –

+0

条件付きフォーミュラ式のVBAマクロを使用せずに行うこともできます。http://stackoverflow.com/questions/29855647/check-if-cell-contains-non-alpha-characters-in-excel – Slai

答えて

4

このタスクには正規表現を使用できます。

ここで便利な正規表現構文はnegated character classです:[^...]を使用し、そこに一致させたくない範囲を挿入します。したがって、ASCII文字、数字、およびハイフン以外の文字と一致させるには、[^a-zA-Z0-9-]を使用します。

そして、正規表現なし

Dim strPattern As String: strPattern = "[^a-z0-9-]" 
Dim regEx As Object 

Set regEx = CreateObject("VBScript.RegExp") 
regEx.Global = True 
regEx.IgnoreCase = True 
regEx.Pattern = strPattern 

For Each cell In ActiveSheet.Range("C:C") ' Define your own range here 
    If strPattern <> "" Then    ' If the cell is not empty 
     If regEx.Test(cell.Value) Then ' Check if there is a match 
      cell.Interior.ColorIndex = 6 ' If yes, change the background color 
     End If 
    End If 
Next 
1

ようにそれを使用する:

このマクロプロセスカラムB

Sub marine() 
    Dim r As Range, rng As Range, s As String 
    Dim i As Long, L As Long 

    Set rng = Intersect(Range("B:B"), ActiveSheet.UsedRange) 

    For Each r In rng 
     If r.Value <> "" Then 
      s = Replace(r.Text, "-", "") 
      L = Len(s) 
      For i = 1 To L 
       If Not Mid(s, i, 1) Like "[0-9a-zA-Z]" Then 
        r.Interior.Color = vbYellow 
       End If 
      Next i 
     End If 
    Next r 
End Sub 

それは数字のみ、大文字と小文字を受け入れます、そしてダッシュ。

+1

あなたの答えの下落は私を混乱させるので、私は何の問題も見当たりません。 Downvotersは、この答えが役に立たない理由について何らかのヒントを与えてください。 – Slai

+0

@Slai Theeはどこかのバグかもしれません.............私は答えを再検討します。 –

+0

私はあなたの答えをdownvoteしなかったあなたの時間のおかげで – Deluq