VBA Office: Ошибка компиляции: Далее без For - PullRequest
0 голосов
/ 26 февраля 2019

Я не уверен, что из-за моего кода вызывает ошибку компиляции.Я сделал отступ и сделал все остальное, что могу придумать, чтобы заставить его работать, но этого не происходит.Ошибка в VBA выделяет первое следующее, что происходит при нахождении среднего.Я использую MBP, но сомневаюсь, что это как-то связано с этой ошибкой.

Sub Stdev()

'Declare my variables
Dim N, i, j As Integer
Dim sumofnum, sumofEsq, Xbar, Variance As Double

'Initialize variables to zero
sumofnum = 0
sumofEsq = 0
Xbar = 0
Variance = 0

'Initialize N, the number of measurements in the sample

N = 15

'Calulate the average Xbar using a For Next loop. The i increments by 1 each time through the ‘loop when it reaches the Next statement.

For i = 1 To N
Do Until (Cells(i, 2).Value = False)
    If (Cells(i, 2).Value = True) Then
        sumofnum = sumofnum + (Cells(i, 2).Value)
    End If
        Cells(i, N).Value = 15
Next

'Cells in vba is used to reference the cells in the spreadsheet by (row, column)
Xbar = sumofnum / N

'Calculate the variance. Here i increments across each column in the data.
For j = 1 To N
   sumofEsq = sumofEsq + (Cells(j, 2).Value - Xbar) ^ 2

Next

'Calculate the population standard deviation
‘Variance = sumofEsq / N

'if you want the sample standard deviation
Variance = sumofEsq / (N - 1)
StdDev = Sqr(Variance)
Cells(2, 18).Value = StdDev

End Sub

Ответы [ 2 ]

0 голосов
/ 26 февраля 2019

Несколько вещей неправильно:

  • Ваши for петли должны заканчиваться в формате Next <what>

  • и ваш Do цикл должен заканчиваться Loop Until <condition>

альтернативно Do Until <condition> ... code.... Loop

Таким образом, ваш код должен выглядеть примерно так:

For i = 1 To N
  Do Until (Cells(i, 2).Value = False)
    If (Cells(i, 2).Value = True) Then
        sumofnum = sumofnum + (Cells(i, 2).Value)
    End If

    Cells(i, N).Value = 15
  Loop 
Next i

'Cells in vba is used to reference the cells in the spreadsheet by (row, column)
Xbar = sumofnum / N

'Calculate the variance. Here i increments across each column in the data.
For j = 1 To N
   sumofEsq = sumofEsq + (Cells(j, 2).Value - Xbar) ^ 2
Next j
0 голосов
/ 26 февраля 2019

Итак, дальше к моему комментарию.

Do Until (Cells(i, 2).Value = False)
    'statements to repeat
Loop 
...