3

. I have an excel spreadsheet which contains some strings with unicode control characters that aren't visible in Windows 7. Therefore, I would like to write a macro to iterate through each cell in a column, checking if a control character is present. If a control character is found, I would like to populate the adjacent cell in the next column with the character name and the index it can be found within the string.

This is what I have so far:

Sub control_chr()
'
' control_chr Macro
'

'
    Dim control_characters(Chr(28), Chr(29), Chr(30), Chr(31), Chr(32))
    Dim r As Range, cell As Range

    Set r = Range("F4:F1029")

    For Each cell In r
        For Each Character In control_characters

the next step would be to search the cell for each character and populate adjacent cells with the results. My first thought was to use the SEARCH() function since it returns the index of where the character is found. This is my first time using visual basic and I'm not sure how to proceed

1
  • Are you interested in a non-VBA solution? Commented Aug 15, 2013 at 14:45

2 Answers 2

1

Here's some code that does what you asked:

Sub ListControlChars()
Dim control_characters As Variant
Dim r As Range, cell As Range, ResultCell As Range
Dim CharPosition As Long
Dim i As Long

control_characters = Array(28, 29, 30, 31, 32)
Set r = ActiveSheet.Range("F4:F1029")

For Each cell In r
    Set ResultCell = cell.Offset(0, 1)
    ResultCell.ClearContents
    CharPosition = 0
    For i = LBound(control_characters) To UBound(control_characters)
    CharPosition = InStr(cell, Chr(control_characters(i)))
        If CharPosition > 0 Then
        ResultCell = ResultCell.Value & "Char " & control_characters(i) & ": Position " & CharPosition & " - "
        End If
    Next i
Next cell
End Sub

If you want to do it in Excel you could set it up like this:

enter image description here

The formula in B2 is:

=IFERROR(SEARCH(CHAR(B$1),$A2),"")
Sign up to request clarification or add additional context in comments.

5 Comments

This seems to do exactly what I'm looking for, but I cant get it to run. I keep getting a type mismatch error on the following line: CharPosition = Instr(cell, Chr(control_characters(i))) This is my first time working with vba, not sure what could be causing this
Also, is it the first cell in the range, i.e., F4?
Yes, first cell is in the range. Cell isn't getting assigned a proper value, getting "Error 2015"
Let me ask another way. What's the address of cell when you get the error? And what is the actual value in that cell when you get the error?
nevermind, I got it. One of the cells had a null value. Thank you for your help
0

I can't recall if I coded this up or found it somewhere else, but this works for me when encountering issues with loading data into certain databases which fail on unrecognised special characters

Function IterateThruCells()    
Dim cell As Range    
   For Each cell In ActiveSheet.UsedRange.Cells
       If cell.Value <> "" Then
          If ContainsSpecialCharacters(cell.Value) = True Then
              Debug.Print cell.Address & ": " & cell.Value
          End If
       End If
   Next
End Function


               
Function ContainsSpecialCharacters(str As String) As Boolean
Dim I
For I = 1 To Len(str)
    ch = Mid(str, I, 1)
    Select Case ch
         Case "0" To "9", "A" To "Z", "a" To "z", " ", "(", ")", "/", "-", ".", ",", "_", "&", "'", "$", ">", "<", "–"
         ContainsSpecialCharacters = False
            
    Case Else
         ContainsSpecialCharacters = True
         Exit For    
    End Select
           
Next
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.