> How can i convert a vba function from absolute to relative?

How can i convert a vba function from absolute to relative?

Posted at: 2014-12-18 
Not sure what you mean by "relative" but from your code I am guessing that you want your sub to be applicable to any row, not just row 3. If so, then I suggest assigning the selected row number to a variable then using this variable in cell addresses.

Try the following:

Public Sub code()

Dim x, y, z As Long, myRow as Integer

myRow = Selection.Row

z = Cells(myRow, 45).Value

x = Cells(myRow, 40).Value

y = Cells(myRow, 43).Value

If x <> 1 Then

If y = x - 1 Then

Cells(myRow, 46).Value = 120

Else

While y <> (x - 1)

Cells(myRow, 46).Value = Cells(myRow, 8).Value / z

Wend

End If

Else

Cells(myRow, 47).Value = Cells(myRow, 11).Value

End If

End Sub

I would like to convert this function from absolute to relative but i dont know how, can someone help me please?

the function :

Public Sub code()

Dim z As Long

z = Range("AS3").Value

Dim x As Long

x = Range("AN3").Value

Dim y As Long

y = Range("AQ3").Value

If x <> 1 Then

If y = x - 1 Then

Range("AT3").Value = 120



Else



While y <> (x - 1)

Range("AT3").Value = Range("H3").Value / z

y = y + 1

Wend



End If

Else

Range("AU3").Value = Range("K3").Value

End If

End Sub