number even, odd, zero and more than 10 how to find?

Asked

Viewed 546 times

0

For g = 1 To 10
For f = 1 To 10

  Cells(f, g) = Int(100 * Rnd())
  k = Cells(f, g)

 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 f
Next g
  • Isn’t it better to read each cell, see if it has value, and compare the value with what you need? For odd numbers, just see if the rest of the division by 2 is 1. I’m putting the logic to see if it helps you, since I don’t know Vb.

  • I’ve tried but I can’t.

  • It was not clear to me the use of the variable W. So I made the example only for the variable K. If it is W or both, just adapt the code. Luana, edit your question not to use photos or "Printscreen" in the case of codes, you can do the same for results. So: in the header of the question text, there are the brackets "{ }", use to "paste" or enter the code you made. There is the icon to insert "images". You can "copy" the area of the worksheet you want to show and "paste" in this area. You can also edit the "Printscreen" screen in Word with "Format" and "Cut" and paste in this area.

1 answer

1


As an example, I only used the variable K, can do the same for other variables if applicable.

Before the "loop" zeroes the variables"

Pares = 0

Impares = 0

Zeros = 0

MaioresQueDez = 0

Place this inside the "loop" and/or adapt to other variables:

    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

As there were modifications in the question, I tested your code and it is correct, but change the outputs as shown below, because the way you did, is overlapping values in the matrix.

Put this after the "loop":

    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"
  • Hi Luana, did you solve with the explanation above? It would be nice to edit your question by doing what I suggested in the other comment, so you’ll get the way to deal with the issues here in Soft.

  • Hi Leo, I did here but it didn’t work out some numbers other did not!

  • But what went wrong? There is no flaw in the code. Did you put it in the indicated positions? There are three parts: before the loop (for next), in the middle and after the loop. Give more information if you have difficulty, and let us know if it works. It may be because of the cells I used the same as yours, I will edit the third part (the output of the values), I think it overlapped and you are not seeing full. See if the answers turned up completely.

  • As such ( for next ) I don’t understand ?

  • You have two, For F = ... For G = ... ... Next G ... Next F. These are the loops.

  • Luana, copy your code to the question (edit it), delete the photo and give 4 spaces at least in each line of your code in the text, so it will understand and show as "code on screen". Then I take your code and add-on with mine, but it needs to be in text format as indicated for me to adapt it.

  • I’ve edited like I did.

  • Luana, see my answer, there I explain, worked what you did, but need to change the final part as I show to understand the overlap and then adapt as you want. Make sure you dial back, and we’ll move on to the next...

  • Leo, now it’s really worked out Thank you so much for helping me!!

Show 4 more comments

Browser other questions tagged

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