If k > 36 Then k = k - 19 End If

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
986
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone.
I have a piece of code that I'm working on, and I need to add a new condition to it. I've tried a few different approaches, but so far none of them have worked. I'm looking for help with both the code itself and the logic I'm using to try to solve the problem.

This is what I have
VBA Code:
Sub L101()

For j = 2 To 6
    For k = 2 To 25
    
        Cells(k, j + 7).Formula = "=trunc(SQRT(" & Cells(k, j).Address & "^2 + " & Cells(k + 1, j).Address & "^2))"
        
        If k > 36 Then
        k = k - 19
        End If
    
    
    Next
Next


End Sub
I tried to create a variable for Cells(k, j + 7).Formula but do not work, so now I am trap in the logic here
how can I control the results

so the code without the condition work, but I need the correction.
this is the results without the condition
ALLCODES.xls
BCDEFGHIJKLM
1
2811131434811242843
31321252789253235
489142023810233142
545192436815263748
6815182933816223247
726141534516263547
Sheet15
Cell Formulas
RangeFormula
I2I2=TRUNC(SQRT($B$2^2 + $B$3^2))
J2J2=TRUNC(SQRT($C$2^2 + $C$3^2))
K2K2=TRUNC(SQRT($D$2^2 + $D$3^2))
L2L2=TRUNC(SQRT($E$2^2 + $E$3^2))
M2M2=TRUNC(SQRT($F$2^2 + $F$3^2))
I3I3=TRUNC(SQRT($B$3^2 + $B$4^2))
J3J3=TRUNC(SQRT($C$3^2 + $C$4^2))
K3K3=TRUNC(SQRT($D$3^2 + $D$4^2))
L3L3=TRUNC(SQRT($E$3^2 + $E$4^2))
M3M3=TRUNC(SQRT($F$3^2 + $F$4^2))
I4I4=TRUNC(SQRT($B$4^2 + $B$5^2))
J4J4=TRUNC(SQRT($C$4^2 + $C$5^2))
K4K4=TRUNC(SQRT($D$4^2 + $D$5^2))
L4L4=TRUNC(SQRT($E$4^2 + $E$5^2))
M4M4=TRUNC(SQRT($F$4^2 + $F$5^2))
I5I5=TRUNC(SQRT($B$5^2 + $B$6^2))
J5J5=TRUNC(SQRT($C$5^2 + $C$6^2))
K5K5=TRUNC(SQRT($D$5^2 + $D$6^2))
L5L5=TRUNC(SQRT($E$5^2 + $E$6^2))
M5M5=TRUNC(SQRT($F$5^2 + $F$6^2))
I6I6=TRUNC(SQRT($B$6^2 + $B$7^2))
J6J6=TRUNC(SQRT($C$6^2 + $C$7^2))
K6K6=TRUNC(SQRT($D$6^2 + $D$7^2))
L6L6=TRUNC(SQRT($E$6^2 + $E$7^2))
M6M6=TRUNC(SQRT($F$6^2 + $F$7^2))
I7I7=TRUNC(SQRT($B$7^2 + $B$8^2))
J7J7=TRUNC(SQRT($C$7^2 + $C$8^2))
K7K7=TRUNC(SQRT($D$7^2 + $D$8^2))
L7L7=TRUNC(SQRT($E$7^2 + $E$8^2))
M7M7=TRUNC(SQRT($F$7^2 + $F$8^2))

as you can see, the numbers 43 and the others greater than 36 are an error.
thank for reading.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
What is the actual logic problem you are trying to solve?
Looks like your loop only provides for cells B2-F26 being calculated at I2-M25
The code looks to calculate c from the formula c^2= a^2 + b^2 using a TRUNC(SQRT())
What is the actual issue here?
 
Upvote 0
Maybe
VBA Code:
Sub Test()
For j = 2 To 7
    For k = 2 To 6
        s = "=TRUNC(SQRT(" & Cells(j, k).Address & "^2 + " & Cells(j + 1, k).Address & "^2))"
        Cells(j, k + 7).Formula = IIf(Evaluate(s) > 36, s & "-19", s)
    Next
Next
End Sub
 
Upvote 1
Solution
Thank you Tetra201, work perfect, I already mark as solution, great detail, and new for me.
so with Cells(j, k + 7).Formula = IIf(Evaluate(s) > 36, s & "-19", s) you don't need
if___then__ end if, awesome
where is the theory about this kind of lines.
Thanks
Tetra201
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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