Creakush Coliko
Creakush Coliko

Reputation: 103

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?

Upvotes: 1

Views: 775

Answers (1)

Vityata
Vityata

Reputation: 43585

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.

Upvotes: 5

Related Questions