how do I sum all the numbers in each row and column?

Asked

Viewed 722 times

0

I am trying to sum up all the numbers of each row and column. I tried to do as below, but it is not working.

ranger .ClearContents

For n = 1 To 10
For Z = 1 To 10

  Cells(Z, n) = Int(100 * Rnd())
  k = Cells(n, Z)

 If Int(k / 2) - k / 2 = 0 Then
        pares = pares + 1
    Else
        Impares = Impares + 1
    End If

    If k = 0 Then Zeros = Zeros + 1

    If k > 10 Then MaioresQuedez = MaioresQuedez + 1

Cells(12, 2) = pares & " números Pares"

Cells(14, 2) = Impares & " números Ímpares"

Cells(16, 2) = Zeros & " números Zero"

Cells(18, 2) = MaioresQuedez & " números maiores do que 10"

next z
next n

Can someone help me? Thank you.

  • Wouldn’t k = Cells(Z,n) ?

  • is correct like this, I just want to know which command to use to add each row and column.

  • See if I get it. You start with a clean sheet. Then you enter a loop and start assigning random values to columns. Assigns a value to cell Z, n. In the next line you assign to variable K the contents of cell n, Z which is currently empty. That’s correct that?

  • I already edited there, I should ask you another question!

  • 1

    Luana, you are opening several questions very similar or very close. Please don’t do this. Even though you may have different doubts, simply pasting the same code doesn’t help. If you haven’t done it yet, do the [tour] and mainly read [Ask].

  • 1

    Another thing: if you want to add an interval (range), why not do it directly on the spreadsheet (selecting cells or using the function SUM)? By the way, even if you need implement something in Excel, if you have the range you can also use the function in the code (see that link).

Show 1 more comment

1 answer

0

Look at it this way:

Dim totalpares
Dim totalimpares
Dim maioresquedez

totalpares = 0
totalimpares = 0
maioresquedez = 0

For n = 1 To 10
For Z = 1 To 10

  Cells(Z, n) = Int(100 * Rnd())
  k = Cells(Z, n)

 If Int(k / 2) - k / 2 = 0 Then
        pares = pares + 1
        totalpares = totalpares + k
    Else
        Impares = Impares + 1
        totalimpares = totalimpares + k
    End If

    If k = 0 Then Zeros = Zeros + 1

    If k > 10 Then
       MaioresQuedez = MaioresQuedez + 1
       maioresquedez = maioresquedez + K
    end if 


Cells(12, 2) = pares & " números Pares, totalizando " & totalpares

Cells(14, 2) = Impares & " números Ímpares, totalizando " & totalimpares

Cells(16, 2) = Zeros & " números Zero"

Cells(18, 2) = MaioresQuedez & " números maiores do que 10. Totalizando " &  maioresquedez

Next Z
Next n

End Sub
  • in this case is the total, but wanted to know how to get the total of each row and column next. thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.