How to dynamically change range inside formula?

Viewed 390

I’m applying a formula: enter image description here

Textual representation of formula:

=(SUBSTITUTE((LEFT(A2;(FIND("htt";A2;1))-3));";";";"))&RIGHT(A2;(LEN(A2)-(FIND("htt";A2;1))+3))

to all cells in a range A2:A10, writing a result to range B2:B10 respectively.

To do this I use the following macro:

Sub ColumnsFormat()
    Dim s As String
    Dim i As Integer, j As Integer
    'Set wb = Workbooks("CSV_File.xlsm")
    Application.ScreenUpdating = False
    j = 1

    For i = 2 To 10
        s = "=(SUBSTITUTE((LEFT(R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],(FIND(""htt"",R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],1))-3)),"","","";""))&RIGHT(R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],(LEN(R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "])-(FIND(""htt"",R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],1))+3))"
        Sheets("Sheet1").Cells(i, 2).Value = s
    Next i
End Sub

The problem is that for some reason a row number inside the formula inside a For cycle is wrong. Instead of taking A2; A3; A4 … A10 cells, (changing row number by 1 each time), macro runs through A2; A4; A6 etc. (increasing a row number by 2 each time).

What am I doing wrong?

1 Answers
Related