修改excel宏以查看整个列而不是单个单元格

时间:2016-03-14 21:18:50

标签: excel vba excel-vba

您好我有一个宏,它会查看sheet1上的文本并查看sheet2上的单词列表。如果sheet1上的sheet2中有任何匹配的单词,则单词将变为红色。如何修改我的下面的宏,以便它查看A列中的所有文本而不仅仅是A3,我在使用Range时不断收到错误(" A:A")。另外,如何使这个宏不考虑大小写?例如:"测试"是在wordbank中,所以如果" test"在sheet1中,它会被突出显示,但不是单词" Test"。

Sub wordbank()
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
For Each c In ws2.Columns(1).SpecialCells(2)
    l = Len(c.Value)
    p = InStr(1, LCase(ws1.[A3].Value), LCase(c.Value))
    Do Until p = 0
        With ws1.[A3]
        b = Asc(.Characters(Start:=p - 1, Length:=1).Text)
        a = Asc(.Characters(Start:=p + l, Length:=1).Text)
        If (a < 97 Or a > 122) And (b = 32 Or b = 34) Then _
          .Characters(Start:=p, Length:=l).Font.Color = vbRed
            p = InStr(p + l + 1, .Value, c.Value)
       End With
    Loop
Next
End Sub

1 个答案:

答案 0 :(得分:2)

像这样:(未经测试)

Sub wordbank()
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
For Each c In ws2.Columns(1).SpecialCells(2)
    l = Len(c.Value)
    For Each cel in ws1.Columns(1).SpecialCells(2)
        p = InStr(1, LCase(cel.Value), LCase(c.Value))
        Do Until p = 0
            With cel
              b = Asc(.Characters(Start:=p - 1, Length:=1).Text)
              a = Asc(.Characters(Start:=p + l, Length:=1).Text)
              If (a < 97 Or a > 122) And (b = 32 Or b = 34) Then _
                .Characters(Start:=p, Length:=l).Font.Color = vbRed
                p = InStr(p + l + 1, .Value, c.Value)
            End With
        Loop
    Next cel
Next c
End Sub