vba the loop are not working.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
985
Office Version
  1. 2010
Platform
  1. Windows
Hello people.
VBA Code:
Sub java_to_vba()
n = 5
      For i = 1 To n
            For j = 1 To n
                  If i = (n / 2) + 1 And j = (n / 2) + 1 Then
                  Cells(i, j) = " + "
                  Else
                  Cells(i, j) = ""
                  End If
            Next
      Next
End Sub
supposedly with this logic a Cruz will be generate
1618781763892.png

It is not working,
Please, your inputs are appreciated
Thanks.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The 'If' line is wrong.
VBA Code:
If i = Int(n / 2) + 1 Or j = Int(n / 2) + 1 Then
 
Upvote 0
When n is an odd number, n/2 is fractional (ends with .5) so the integer i or j will never equal it. Also, you want to use OR (not AND) in that If statement (the only time they would both be True is for the center cell). Change this line of code...

If i = (n / 2) + 1 And j = (n / 2) + 1 Then

to this...

If i = Int(n / 2) + 1 Or j = Int(n / 2) + 1 Then
 
Upvote 0
Solution

Forum statistics

Threads
1,223,713
Messages
6,174,041
Members
452,542
Latest member
Bricklin

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