Sub procedure to create an array

Jessie20

New Member
Joined
Oct 19, 2022
Messages
9
Office Version
  1. 365
Platform
  1. MacOS
Hi,
I need to create a 100 × 100 array, which has ones in its subdiagonal elements and zeros in other elements.
I also have to use the For next statement. I cant get it right after several tries. This is where I am for now. Could you please help me?
Thanks a lot
Capture d’écran, le 2022-10-19 à 14.37.43.png
 

Attachments

  • Capture d’écran, le 2022-10-19 à 14.34.01.png
    Capture d’écran, le 2022-10-19 à 14.34.01.png
    32.1 KB · Views: 11

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You have declared your array to be 1 To 100 but your loop starts at 0. Change your loop to be For i = 1 To 100 and For j = 1 To 100.

Also it helps to paste your code directly in your post, but I see why you used a screenshot to show the highlighted line of code.
 
Upvote 0
Thanks! It still shows the same highlighted line of code though. Is my code wrong to show the number 1 in its subdiagonal elements? When I try to run it, it indicates: Run time error 9 , Subscript out of range. If I press end instead of debug, it shows 1 everywhere.

Sub Question4()
Dim mA(1 To 100, 1 To 100) As Integer
Dim i As Integer
Dim j As Integer

For i = 1 To 100
For j = 1 To 100
mA(i, j) = 0
If i - j = 1 Or -1 Then
mA(i, j) = 1
Else
mA(i, j) = 0
End If
ActiveSheet.Cells(i, j) = mA(i, j)
Next j
Next i


End Sub
 
Upvote 0
Change

Code:
If i - j = 1 Or -1 Then

to

Code:
If i - j = 1 Or i - j = -1 Then

Reason being: Your original
Code:
If i - j = 1 Or -1 Then
is equivalent to
Code:
If (i - j = 1) Or (-1 = true) Then
where -1 *always* equals true, hence why you were getting all 1s.
 
Upvote 0
Solution
Change

Code:
If i - j = 1 Or -1 Then

to

Code:
If i - j = 1 Or i - j = -1 Then

Reason being: Your original
Code:
If i - j = 1 Or -1 Then
is equivalent to
Code:
If (i - j = 1) Or (-1 = true) Then
where -1 *always* equals true, hence why you were getting all 1s.
THANKS!! :) Its working!
 
Upvote 0
When posting about a VBA runtime error always give the error number and error description along with what line of code it occurred on.

Interestingly the issue that Oaktree noted is certainly a bug but I don't see how it could cause a runtime error on the same line indicated in your first post.
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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