Я не уверен, если у вас есть переменные, объявленные ранее или нет.Чтобы избежать того, что они не объявляются, я рекомендую использовать Option Explicit
в верхней части кода.
С другой стороны, я полагаю, что ваша проблема связана с ActiveSheet
в тот момент, когда вызапустив ваш макрос, так как вы не указали полные диапазоны, они переходят на ActiveSheet
, чтобы избежать этого, вы можете прочитать комментарии:
Option Explicit
Sub TesT()
Dim CellRef As String 'not sure if you are using it later
Dim StringLength As Long ' you used Integer, Byte or Long, Integer is a Long cutted.
Dim MyRange As Range 'we will set here the working range
Dim C As Range 'It is always better to loop through cells with For Each
Dim LastRow1 As Long 'Not sure if you declared it before
Dim i As Long 'Not sure if you declared it before
With ThisWorkbook.Sheets("SeetName") 'always full qualify your ranges: workbook-worksheet-range
LastRow1 = .Cells(.Rows.Count, "I").End(xlUp).Row 'last row on column I
Set MyRange = .Range("I2:I" & LastRow1) 'this will be your working range
For Each C In MyRange 'this will loop through all the cells between J2 to J LastRow1
StringLength = Len(C)
For i = 1 To StringLength
If IsNumeric(Mid((C), i, 1)) Then
.Cells(j, "J") = i 'full qualify the range, note the "." before Cells, referencing to the with line
Exit For 'this will exit the loop so only the first match remains.
' i = StringLength + 1 'don't know why did you do this, but if you intend to keep the first position the above line is it.
End If
Next i
Next C
End With
End Sub