简体   繁体   中英

Count special characters in range in a function

This function should count all special hidden characters in selected range.

I got an error. StrIn = C.Text didn't work.

Function hiddencount(Rng As Range)

Dim C As Range
Dim cnt As Integer
Dim iCh As Integer
Dim StrIn As String
cnt = 0

For Each C In Rng.Cells
    C.Value = StrIn

    If Not C.HasFormula Then
        For iCh = 1 To Len(StrIn)
            If Asc(Mid(StrIn, iCh, 1)) < 32 Then
                cnt = cnt + 1
            End If
        Next iCh
    Else
    End If
Next C

hiddencount = cnt

End Function

In short, change C.Value = StrIn to StrIn = C.Text


You really do not need this variable though. It just creates more code to read through later. Why not just use C.Text so it is clear what is being analyzed? Also, if you do not plan on acting on the Else statement, you can just remove it.

Function hiddencount(Rng As Range)

Dim C As Range, cnt As Integer, iCh As Integer

For Each C In Rng.Cells
    If Not C.HasFormula Then
        For iCh = 1 To Len(C.Text)
            If Asc(Mid(C.Text, iCh, 1)) < 32 Then cnt = cnt + 1
        Next iCh
    End If
Next C

hiddencount = cnt

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM