For each loop conditions causing type mismatch

Viewed 189

When the loop conditions include the entire range of Column F, this codes works just fine:

Dim ABnumb As String
Dim cellSkill As Range
Let ABnumb = dayWS.Cells(i1, 2).Value

    For Each cellSkill In skillSheet.Range("F:F")
        If cellSkill.Value = ABnumb Then
            Let skillNumb = cellSkill.Offset(0, -5).Value
            Let skillDelimited = skillDelimited & ";" & skillNumb
        End If
    Next cellSkill

But to increase efficiency, I want to limit the loop to the used range. When I change the code to this:

For Each cellSkill In skillSheet.UsedRange("F")

I get a type mismatch error for this expression:

If cellSkill.Value = ABnumb Then

I played with changing the two variables' declared data types, but nothing availed.

Is there a way I can use .UsedRange("F")? Or should I just resort to .Range(1, lastRow), where lastRow is already determined by the script?

Also, what happens to the For Each variable cellSkill in the loop? Can I reuse this variable for another For Each loop through cell ranges without trouble?

1 Answers
Related