Скрытый текст
Код:
Option Explicit
Sub Sample()
Dim n As Variant
Dim i As Integer
Dim iCount As Integer
Dim objRange As Range
n = InputBox("Enter max row number", "Enter max row number", 25)
If Len(n) > 0 Then
Randomize Timer
With ThisWorkbook.Worksheets.Item(1)
.UsedRange.Clear
iCount = 0
For i = 1 To CInt(n)
.Cells.Item(i, 1).Value = Round(4 * Rnd - 2, 2)
Next i
For i = 1 To CInt(n)
Set objRange = .Cells.Item(i, 2)
Select Case i
Case 1
objRange.Value = Round(Application.WorksheetFunction.Average(.Range(.Cells.Item(i + 1, 1), .Cells.Item(n, 1))), 4)
Case n
objRange.Value = Round(Application.WorksheetFunction.Average(.Range(.Cells.Item(1, 1), .Cells.Item(i - 1, 1))), 4)
Case Else
objRange.Value = Round(Application.WorksheetFunction.Average(Union(.Range(.Cells.Item(1, 1), .Cells.Item(i - 1, 1)), .Range(.Cells.Item(i + 1, 1), .Cells.Item(n, 1)))), 4)
End Select
If objRange.Value < 0 And objRange.Row Mod 2 Then
iCount = iCount + 1
End If
Next i
End With
MsgBox "Total found:" & CStr(iCount), vbInformation + vbOKOnly, "Total found"
End If
End Sub
|