Visual Basic (Excel)
Podstawy
Edytor Visual Basic
...
Uruchomienie programu
Widok edytora VBA
Wstawianie
formularza
Uruchamianie
Własności
komponentów
Formular
z
Komponent
y
Umieszczanie komponentów na arkuszu ...
1
2
3
4
Elementy języka Visual Basic
Zmienne:
Typ całkowity:
• Integer
• Long
Typ rzeczywisty:
• Single
• Double
Typ łańcuchowy:
• String
Typ logiczny:
• Boolean
Typ dowolny
• Variant
Deklaracja zmiennych:
Dim Nazwisko As String*20
Dim x,y As Double
Instrukcja przypisania:
X = 3.5
Imie = ”Jan”
Suma = X+Y
K = K+1
Wyrażenia logiczne:
• =
• <>
• <
• >
• <=
• >=
• and
• or
Wyrażenia arytmetyczne:
+
- dodawanie
-
- Odejmowanie
*
- Mnożenie
/
- dzielenie
\
- dzielenie całkowite
^
- potęgowanie
MOD
- reszta z dzielenia
Wyrażenia łańcuchowe:
Dim nazwa As String*20
Dim napis As String
nazwa = ”Tekst”
Napis = ”To jest ” & nazwa
Instrukcja if:
Dim x,y,x As Single
.........
If y<>0 Then
z=x/y
MsgBox ”wynik dzielenia = ” &
Str(z)
Else
MsgBox ”nie dziel przez zero”
End If
Instrukcja wyboru Select Case
Dim ocena As Integer
ocena = InputBox("Podaj
ocenę")
Select Case ocena
Case 5
MsgBox ("bardzo dobra")
Case 4
MsgBox ("dobra")
Case Else
MsgBox ("inna")
End Select
Pętla for
Dim i As Integer
For i = 1 To 3
MsgBox i
Next i
Pętla Do While
Do While warunek
instrukcje
Loop
Do
instrukcje
Loop While warunke
Tablice jednowymiarowe
Dim potegi(10) As Double
Dim k As Integer
For k=0 to 10
potegi(k) = 3^k
Next k
Tablice wielowymiarowe
Dim tabela(1 To 4, 1 To 8) As
Integer
Funkcje
Function Modul(x As Double) As
Double
If x < 0 Then
Modul = -x
Else
Modul = x
End If
End Function
Procedury
Sub Komunikat(kom As String)
MsgBox (kom)
End Sub
Interpolacja
Lagrange’a
procedure TForm1.Lagrange(n: Integer; x: Real;
var y: Real;
xw,yw: Tabl1);
var
i,j: Integer;
il1,il2,xx: Real;
begin
y:=0;
for i:=0 to n do begin
xx:=xw[i];
il1:=1; il2:=1;
for j:=0 to n do
if j<>i then begin
il1:=il1*(x-xw[j]);
il2:=il2*(xx-xw[j]);
end;
y:=y+yw[i]*il1/il2;
end;
end;
Function Lagrange(ByVal n As Integer, ByVal x As
Double, _
xw() As Double, yw() As Double) As Double
Dim a, j As Integer
Dim il1, il2, xx, y As Double
y = 0
For i = 0 To n
xx = xw(i)
il1 = 1
il2 = 1
For j = 0 To n
If j <> i Then
il1 = il1 * (x - xw(j))
il2 = il2 * (xx - xw(j))
End If
Next j
y = y + yw(i) * il1 / il2
Next i
Lagrange = y
End Function
Delphi
Visual Basic
Sub Przycisk1_Kliknięcie()
Dim xw(1000) As Double
Dim yw(1000) As Double
Dim i, j, n, m As Integer
Dim a, b, x, h As Double
n = Val(Application.Cells(3, 1))
m = Val(Application.Cells(6, 1))
a = Val(Application.Cells(9, 1))
b = Val(Application.Cells(12, 1))
For i = 0 To n
xw(i) = Val(Application.Cells(3 + i,
2))
yw(i) = Val(Application.Cells(3 + i,
3))
Next i
h = (b - a) / m
For i = 0 To m
x = a + (i * h)
y = Lagrange(n, x, xw, yw)
k = 4
w = 3 + i
Application.Cells(w, k) = (x)
k = 5
w = 3 + i
Application.Cells(w, k) = (y)
Next i
End Sub
Koniec