简体   繁体   中英

Excel VBA. For each row in range, for each cell in row, for each character in cell

I have created a VBA macro simple to absurdity.

For each row in range, then for each cell in row and then for each character in cell.

But it returns property or method not supported.

Now, to refer to each character in a cell in Excel VBA, it has to be defined as a range. So how is not a cell in a row in a range not a range object?

In all Excel examples the cells with Characters property have been referred to in absolute values. Like Worksheets("Sheet1").Range("A1")

Do I have to write a code of Worksheets("MySheet").Range("B1") ... Worksheets("MySheet").Range("B2") ... ... Worksheets("MySheet").Range("B250") To access characters or is there a way to access each character in a cell without Mid function?

By the way, Mid function looses formatting but I am looking for underlined characters.

My program is like this

Sub Celltest()
    For Each rw In Sheets("Joblist").Range("B1:D250").Rows
        For Each cel In rw.Cells
            For Each char In cel.Characters
                If char.Font.Underline = True Then MsgBox char
            Next
        Next
    Next
End Sub

As a result I get message that a property or method is not supported.

Thank you!

David

"Why is suddenly a part of a range object suddenly not a range?" That's not the issue. The error is Object doesn't support this property or method . That means that (basically) anything after the . is an object, so the error is telling you that something is up with how you're using .Character .

I did some quick searching around, and this works:

Sub Celltest2()
Dim rw As Range, cel As Range
Dim i As Integer
Dim char    
    For Each rw In Sheets("Joblist").Range("B1:D250").Rows
        For Each cel In rw.Cells
            For i = 1 To Len(cel)
                'Debug.Print cel.Characters(i, 1).Text
                If cel.Characters(i, 1).Font.Underline = 2 Then
                    MsgBox (cel.Characters(i, 1).Text)
                End If
            Next i
        Next
    Next
End Sub

I just looked up the Characters Object documentation, and there is the answer of how to use .Characters . The issue is how .Characters is used with the range - no sophisticated skills or knowledge necessary. Just use the VB Error messages.

Two things I noticed. 1. Characters is not a collection, so For Each won't work. 2. Font.Underline is not Boolean. Try something like:

Sub Celltest()
    Dim rw As Excel.Range
    Dim cel As Excel.Range
    Dim char As Excel.Characters
    Dim I As Long
    For Each rw In Sheets("Joblist").Range("B1:D250").Rows
        For Each cel In rw.Cells
            For I = 1 To cel.Characters.Count
                Set char = cel.Characters(I, 1)
                If char.Font.Underline <> xlUnderlineStyleNone Then MsgBox char.Text
            Next I
        Next
    Next
    Set rw = Nothing
    Set cel = Nothing
    Set char = Nothing
End Sub

Hope that helps

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