String selection in vba task

I programmatically select a line in VBA, but this gives me a TYPE MISMATCH error:

Rows(Str(i) & ":" & Str(i)).Select

what am I doing wrong?

For i = 5 To 1000
    If Worksheets("5470").Cells(i, 2) = "" Then
        Rows(Str(i) & ":" & Str(i)).Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Delete Shift:=xlUp
        Exit For
    End If
Next i
+3
source share
2 answers

Why not use

Rows(i).Select

Afaik, lines can also be indexed by line number.

Alternative:

Cells(i, 1).EntireRow.Select
+10
source

The error you get is that the STR functions add space to the number. So, when I = 100, you get "100: 100". You can use the GolezTrol method or use cstr () instead of str (). Space is added to account for a possible negative value.

+5
source

Source: https://habr.com/ru/post/1783481/


All Articles