简体   繁体   中英

vba in excel 2003

I'm trying to understand this code, and I still don't know why it starts with 2 on the cell A1, shouldn't it start with 1?

Private Sub CommandButton1_Click()
Dim i, j As Integer
For i = 1 To 10
For j = 1 To 5
Cells(i, j).Value = i + j
Next j
Next i
End Sub

because on my other example I have this and it starts with 1 in A1:

  Private Sub CommandButton1_Click()
  Dim i As Integer
  For i = 1 To 10
  Cells(i, 1).Value = i
  Next
  End Sub

Thanks :-) will greatly appreciate your help

Both the loops in the first example starts with 1, so setting the value to i+j = 2.

In the second example it only sets it to i, which starts at 1.

At the first example you have two for-loops, one nested in another. You basically form a 2D array, with the value of each cell being the sum of the row and column index.

At the second example you have a 1D array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM