Dim x As Single, y As Single, w As Single, n As Single

x = Val(InputBox(«Введите x»))

n = Val(InputBox(«Введите n»))

y = Exp(-2 * x) + 1

z = Log(x) / (x + 1)

If x < z ^ 2 Then w = Sqr(x * y) Else w = n * x + 2

Msgbox (“w=” & w)

В этой программе можно вывести результат в ячейку на рабочем листе Excel. Для этого вместо MsgBox следует записать

Worksheets(“Лист1”).Range(“a1”)=w

Значение w будет выведено в ячейку А1 на рабочем листе с именем Лист1.

Разветвляющиеся программы со сложными условиями

Пример 2 программы вычислений значения функции y со сложным условием.Значения n и xвводятся с клавиатуры:

Dim x As Single, y As Single, w As Single, n As Single - student2.ru .

Private Sub CommandButton1_Click()

Dim x As Single, n As Single, y As Single

'Ввод исходных данных

x = Val(InputBox("Введите x "))

n = Val(InputBox("Введите n"))

'Проверка условий и расчет значений

If x >= 0 And n >= 0 Then y = Sqr(x)

If x < 0 And n < 0 Then y = n * x + 2

MsgBox ((“y=” & y)'Вывод результата

End Sub

Пример 3 программы вычисления значения функции yс вложенным оператором If:

Dim x As Single, y As Single, w As Single, n As Single - student2.ru.

Private Sub CommandButton1_Click()

Dim x As Single, y As Single

x = Val(InputBox("Ввелите x "))

If x<0 Then y =x+5 Else If x<=5 Then y=Sqr(3*x) Else y =x^2

MsgBox (y)

End Sub

Пример 4 (использование блочного оператора If).Вычислить значения y=sin2x, w=ctgx, если х <5; y=1-sinx, w=arctgx, если х ³ 5.

х={9; 0.1; -4; 5; 12}

Private Sub CommandButton1_Click()

Dim x As Single, y As Single, z As Single

Dim w As Single, I As Integer

For i=1 to 5

x = Val(InputBox("Введите значение х"))

'Проверка условий и расчет значений

If x > 5 Then

y = Sin(x)^2

w = cos(x)/sin(x)

Else

y = 1 - Sin(x)

w = Atn(x)

End If

MsgBox (“x=” & x & “ y=” & y & “ w=” & w)

Next

End Sub

Пример 5. Вычислить значение функции n:

Dim x As Single, y As Single, w As Single, n As Single - student2.ru

, x=1,7 s=5,2 значение j задать самостоятельно.

Оформим Лист2 согласно образцу (рис. 6.1). Переименуем «Лист2» в «Условный оператор», вставим расчетные формулы и введем исходные данные.

Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru

Рис. 6.1 − Образец оформления программы с разветвлением на листе Excel

Для кнопки «Расчет If» напишем следующую процедуру:

Private Sub CommandButton1_Click()

Worksheets("Условный оператор").Range("c2") = "Вычисления с разветвлением" ‘вносит текст в ячейку «С2» листа «Условный оператор»

Dim x As Single, s As Single, v As Single, j As Single

x=Val(InputBox(“Введите значение х”))

s=Val(InputBox(“Введите значение s”))

j = Worksheets("Условный оператор ").Range("i19")

If x * j < 2 * s Then v = Cos(x * j) ^ 2 Else If x * j >= 2 * s And _

x * j <= 3 * s Then v = 2 * Tan(j * x) Else v = 5 - Exp(x / 2)

MsgBox ("Значение v=" & Format(v, "##.####"))

End Sub

Для проверки результата вычисления надо ввести в ячейку D23 формулу:

=ЕСЛИ(D19*I19<2*F19;COS(I19*D19)*COS(I19*D19);ЕСЛИ(И(D19*I19>=2*F19;D19*I19<=3*F19);2*TAN(I19*D19);5-EXP(D19:D19/2)))

Результат вычислений, полученный с помощью формул Excel и процедуры VBA должен совпасть.

Содержание заданий

1. Выполнить приведенные выше примеры программ с разветвлением.

2. Составить программу по условиям, приведенным в табл. 6.1.

Таблица 6.1

Формула для вычислений Исходные данные
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru
  Dim x As Single, y As Single, w As Single, n As Single - student2.ru Dim x As Single, y As Single, w As Single, n As Single - student2.ru

Наши рекомендации