G
G
ghostkhimki20152021-06-12 21:25:28
Visual Basic
ghostkhimki2015, 2021-06-12 21:25:28

VBA Excel how to correctly make a macro to determine N - the number of xi with even numbers less than x1 and xmax / 2?

Hello! I have a task

I need to do in VBA in Excel
Given x1, x2, ..., x8.
To determine N - the number of xi with even numbers less than x1 and xmax / 2

, I tried to do it myself, but it turned out like this =(

Dim a
a = Array(10, 9, 8, 7 ,6, 5, 4, 3, 2, 10)
counter = 0
size = 10
answer = 0

For Each num in a
  counter = counter + 1
  
  if (num<a(0)) And (num<(a(9)/2)) And ( counter Mod 2 = 0 )  then
  answer = answer + 1
  End If
Next
WScript.StdOut.WriteLine answer


Plus, you still need to do a "calculation" in the function. Am I moving in the right direction?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-06-13
@BasiC2k

Dim i as Integer, iMax as Integer, N as Integer, iCnt1 as integer, iCnt2 as integer
Dim a
a = Array(10, 9, 8, 7 ,6, 5, 4, 3, 2, 10)
For i = LBound (a) to UBound(a) ' Find the maximum value
If iMax < a(i) then iMax = a(i)
Next
iMax = iMax / 2
N = 0 ' Number of even elements
iCnt1 = 0' Number less than x0
iCnt2 = 0 ' Quantity less than iMax / 2
For i = LBound(a) to UBound(a) ' Check conditions
If i Mod 2 = 0 then N=N+1
If a(i) If a(i) Next
Msgbox "Number of even elements " & N & vbcrlf & _
"Number less than x0 " & iCnt1 & vbcrlf & _
"Number less than iMax / 2 " & iCnt2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question