0

As the title says, I want to loop from cell i to the last non-empty cell. But I keep getting a type mismatch error even though I am declaring it the same type.

Dim wsList as Worksheet
Dim wsCode as Worksheet

Dim i as Variant
Dim j as Long

Dim LastCell as Variant
Dim LastCell2 as Long

With wsList 
    LastCell = .Cells(.Rows.Count, "G").End(xlUp) 
    LastCell2 = .Cells(.Rows.Count, "F").End(xlUp)
End With

    For i = 1 To LastCell
        wsList.Range("G2").Offset(i - 1).Copy _
            wsCode.Range("C2").Offset((i - 1) * 14)
    Next i

    For j = 1 To LastCell2
        wsCode.Range("F11").Offset((j - 1) * 14).Value = _
            wsList.Range("F2").Offset(j - 1).Value
    Next j

End Sub

Mismatch error occurs between LastCell and i even though I declared them both as variant. I also tried declaring them as string but still getting the same error. The data type in that particular column is "AB12345" including quotation marks. How can I fix this?

Thanks in advance!

2 Answers2

1

As stated in the comments, you're missing a .row. It would be best to define all your row variables as Long. One other thing to consider is that your method of using end(xlUP) will not account for any rows that are hidden. This answer discusses a variety of ways to address this.

With wsList 
    LastCell = .Cells(.Rows.Count, "G").End(xlUp).row
    LastCell2 = .Cells(.Rows.Count, "F").End(xlUp).row
End With
pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
  • 1
    That worked, thanks a lot! variant type was okay for both. I will look over the links you mentioned but for now I am not worried about hidden rows as my current sheet doesn't have any hidden rows. – actuarial.codes Jul 22 '21 at 17:34
-1

I think you need to change

Dim LastCell as Variant

To

Dim LastCell as Long

Also, you should clearly identify the sheets for wsList and wsCode.

Dave Lett
  • 88
  • 2