简体   繁体   中英

Excel vba - Split function gives 'Subscript out of range' error inside loop

I have two columns with data. Each cell is formatted: "***" or "XXX at mm.dd.yyyy", where XXX represent various numeric combinations, and I need to replace "XXX at mm.dd.yyyy" with "XXX", so I've done this:

For Each c In Range(.Cells(2, 9), .Cells(finalrow, 10))
        If c <> "***" Then
            c.Value = Split(c, " at")(0) * 1
        End If
Next c

but I get a 'Subscript out of range' error on the row 2345.

What am I missing here?

Concerning that you probably get an error because of the empty 2345 row:

For Each c In Range(.Cells(2, 9), .Cells(finalrow, 10))
        If c <> "***" Then
            If InStr(1, c, " at ") Then
                c.Value = Split(c, " at")(0)
            End If
        End If
Next c

It checks whether there is " at " in c , thus the split will not result in an error.

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