Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

excel - How to use VBA in Google Sheets to highlight cells with special characters and uppercase letters?

I managed to get a VBA-snippet working in Microsoft Excel that highlights all Cells in Range C1 to E10000 which contain anything else than lowercase a–z, numbers and hyphens.

The code looks like this:

Option Explicit
Sub SpecialChars()
    Dim RangeToCheck As Range, c As Range

Set RangeToCheck = Range("C1:E10000")
For Each c In RangeToCheck
    If Len(c.Text) > 0 Then
        If c.Text Like "*[!.a-z0-9-]*" Then
            c.Interior.Color = vbRed
        Else: c.Interior.Color = vbYellow
        End If
    End If
Next c
End Sub

What is the proper way to transfer this to Google Sheets? I think the problem might be, that Google uses JavaScript while Excel uses something else.

Still I wanted to give it a shot here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think you need scripts. You could use conditional formatting. It seems the only reason you're using VBA is because you need REGEX, which Microsoft excel doesn't support except through VBA. Google Sheets however has REGEX support inbuilt.

... highlights all Cells in Range C1 to E10000 which contain anything else than lowercase a–z, numbers and hyphens....

Conditional formatting >Custom formula:

=REGEXMATCH(C1,"^[a-z0-9-]+$")

This will be used to match

  • Lowercase
  • Numbers
  • Hyphen (-)

Highlight yellow
Apply to range: C1:E10000

Note: Spaces are not matched. So, If you have spaces, It will not match.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...