Cells(2, i).Formula = "=trunc(SQRT(" & Cells(2, j).Address & "^2 + " & Cells(3, j).Address & "^2))"

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone.
I have
Book1
BCDEF
1
2811131434
313212527
Sheet1


so the results I expect are

Book1
IJKLM
1
2811242843
Sheet1


and what I have been doing is
VBA Code:
Sub Pytagoras_one()

            Cells(2, 9).Formula = "=trunc(SQRT(" & Cells(2, 2).Address & "^2 + " & Cells(3, 2).Address & "^2))"
            Cells(2, 10).Formula = "=trunc(SQRT(" & Cells(2, 3).Address & "^2 + " & Cells(3, 3).Address & "^2))"
            Cells(2, 11).Formula = "=trunc(SQRT(" & Cells(2, 4).Address & "^2 + " & Cells(3, 4).Address & "^2))"
            Cells(2, 12).Formula = "=trunc(SQRT(" & Cells(2, 5).Address & "^2 + " & Cells(3, 5).Address & "^2))"
            Cells(2, 13).Formula = "=trunc(SQRT(" & Cells(2, 6).Address & "^2 + " & Cells(3, 6).Address & "^2))"


End Sub
so from here I can see a variable 9 to 13 and another 2 to 6 for that then I did
VBA Code:
Sub Pytagoras_two()


For i = 9 To 13
For j = 2 To 6

Cells(2, i).Formula = "=trunc(SQRT(" & Cells(2, j).Address & "^2 + " & Cells(3, j).Address & "^2))"


Next
Next

End Sub
but do not work
so: what I did wrong
thank you for reading this
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
VBA Code:
Sub Pytagoras_two()


For j = 2 To 6

Cells(2, j + 7).Formula = "=trunc(SQRT(" & Cells(2, j).Address & "^2 + " & Cells(3, j).Address & "^2))"


Next

End Sub


This works also. The cell references in the formula will update for each formula.
VBA Code:
Sub Pytagoras_two()

Range("I2:M2").Formula = "=TRUNC(SQRT(B2^2 + B3^2))"

End Sub
 
Last edited:
Upvote 1
Solution

Forum statistics

Threads
1,216,058
Messages
6,128,532
Members
449,456
Latest member
SammMcCandless

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top