Upload
asta
View
71
Download
1
Embed Size (px)
DESCRIPTION
Programowanie w języku Visual Basic dla arkusza kalkulacyjnego Excel. W celu dostosowania arkusza kalkulacyjnego do swoich potrzeb, użytkownik może tworzyć tzw. Makra (skrót od makrokomendy), czyli programy w języku Visual Basic. - PowerPoint PPT Presentation
Citation preview
Programowanie
w języku Visual Basic
dla arkusza kalkulacyjnego Excel.
W celu dostosowania arkusza kalkulacyjnego do swoich potrzeb, użytkownik może tworzyć tzw. Makra (skrót od makrokomendy), czyli programy w języku Visual Basic.
Najprostszą i zarazem najprymitywniejszą formą tworzenia makra jest automatyczne rejestrowanie czynności wykonywanych ręcznie przez użytkownika na arkuszu. Takie makro, po uruchomieniu odtworzy dokładnie wszystkie wykonane operacje.
Podstawowe komendy makr w arkuszu kalkulacyjnym
Wstawianie napisów – ActiveCell.FormulaR1C1 =
Range("B1").Select ActiveCell.FormulaR1C1 = "Obliczenie średniej arytmetycznej"
Range("A3").Select: ActiveCell.FormulaR1C1 = "Nr"
Range("B3"). ActiveCell.FormulaR1C1 = "L"
Parametry tekstu:
Zmiana arkusza: Zmiana na Arkusz2 Sheets("Arkusz2").Select
Rysowanie ramek:
Grubości linii gruba (.Weight = xlThick),średnia (.Weight = xlMedium),cienka (.Weight = xlThin),
Zaznaczenie obszaru: Range("A3:F9").Select Rysowanie linii zewnętrznych (Edge): - lewa ramka (xlEdgeLeft) With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThick End With
- górna ramka (xlEdgeTop) With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThick End With - dolna ramka (xlEdgeBottom) With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThick End With
- prawa ramka (xlEdgeRight) With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThick End With
Linie wewnętrzne (Inside): - linie pionowe (xlInsideVertical) With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin End With
- linie poziome (xlInsideHorizontal) With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlMedium End With
Range("A3:F9").Select With Selection.Borders(xlEdgeLeft).Weight = xlThick .Borders(xlEdgeTop).Weight = xlThick .Borders(xlEdgeBottom).Weight = xlThick .Borders(xlEdgeRight).Weight = xlThick .Borders(xlInsideVertical) .Weight = xlThin.Borders(xlInsideHorizontal). Weight = xlMedium End With
W polach B4:B9 będziemy wpisywać dane, a więc obszar ten nie może być zablokowany ani ukryty.
Range("B4:B9").Select Selection.Locked = False Selection.FormulaHidden = False
Sub srednia() '**************** WYCZYSZCZENIE ZAWARTOŚCI ARKUSZA ***** ActiveSheet.Unprotect Range("A2:F30").Select Selection.Delete Shift:=xlUp '**************** WSTAWIENIE TYTUŁU ******************* Range("B1").Select Selection.Font.Bold = True Selection.Font.Italic = True ActiveCell.FormulaR1C1 = "Obliczenie średniej arytmetycznej "
'******************* PYTANIE O LICZBĘ POMIARÓW *****************
Range("C2").Select
liczba = InputBox("Podaj liczbe wartości do uśrednienia")
lis = Trim(Str(liczba))
’ Str –zamiana liczby na string; Trim – usuwanie zbędnych spacji
'****************** WSTAWIANIE OPISÓW TABELKI ***********************
Range("A3").ActiveCell.FormulaR1C1 = "Nr" Range("B3").ActiveCell.FormulaR1C1 = "L" Range("C3").ActiveCell.FormulaR1C1 = "l" Range("D3").ActiveCell.FormulaR1C1 = "v" Range("E3").ActiveCell.FormulaR1C1 = "vv" Range("A4").ActiveCell.FormulaR1C1 = "1" Range("A5").ActiveCell.FormulaR1C1 = "2"
Range("A3:E3").Select: Selection.HorizontalAlignment = xlCenter
'*************** WSTAWIANIE NUMERÓW POMIARÓW *****************
ls = Trim(Str(liczba + 3)) ra = "A4:A" + ls Range("A4:A5").Select Selection.AutoFill Destination:=Range(ra), Type:=xlFillDefault Range(ra).Select: Selection.HorizontalAlignment = xlCenter
'********** WSTAWIANIE NAPISU Xmin *****************
r3 = "A" + Trim(Str(liczba + 5))
Range(r3).Select
Selection.Font.Bold = True
ActiveCell.FormulaR1C1 = "xmin=„
With ActiveCell.Characters(Start:=2, Length:=3).Font.Subscript = True
End With
Selection.HorizontalAlignment = xlRight
'*********** WSTAWIANIE NAPISU DELTA X **************
r4 = "A" + Trim(Str(liczba + 6))
Range(r4).Select
Selection.Font.Bold = True
ActiveCell.FormulaR1C1 = "Dx="
With ActiveCell.Characters(Start:=1, Length:=1).Font
.Name = "Symbol"
End With
Selection.HorizontalAlignment = xlRight
'********* WSTAWIANIE NAPISU X= **********************
r5 = "A" + Trim(Str(liczba + 7))
Range(r5).Select
Selection.Font.Bold = True
ActiveCell.FormulaR1C1 = "X="
Selection.HorizontalAlignment = xlRight
'********** OBLICZENIE Xmin I NADANIE NAZWY Xmin ************************
r3 = "B" + Trim(Str(liczba + 5))
Range(r3).Select
ActiveCell.FormulaR1C1 = "=MIN(R[-7]C:R[-2]C)"
ActiveWorkbook.Names.Add Name:="xmin"
RefersToR1C1:="=Arkusz1!R" + Trim(Str(liczba + 5)) + "C2"
'*********** FORMATOWANIE PÓL Z DANYMI ******************
rb = "B4:B" + Trim(Str(liczba + 5))
Range(rb).Select
Selection.NumberFormat = "0.00"
Selection.HorizontalAlignment = xlRight
Range("B4").Select
'*********** OBLICZANIE WARTOŚCI l *********************
Range("C4").ActiveCell.FormulaR1C1 = "=(RC[-1]-xmin)*100"
rc = "C4:C" + Trim(Str(liczba + 3))
Range("C4").Select
Selection.AutoFill Destination:=Range(rc), Type:=xlFillDefault
'********** OBLICZANIE SUMY l ***************************
Range("C" + Trim(Str(liczba + 4))).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" + lis + "]C:R[-1]C)"
'********* OBLICZENIE DELTA X ******************
rb = "B" + Trim(Str(liczba + 6))
Range(rb).Select
ActiveCell.FormulaR1C1 = "=R[-2]C[1]/" + lis
ActiveWorkbook.Names.Add Name:="dx",
RefersToR1C1:="=Arkusz1!R" + Trim(Str(liczba + 6)) + "C2"
'********** OBLICZENIE X ************************
rb = "B" + Trim(Str(liczba + 7))
Range(rb).Select
ActiveCell.FormulaR1C1 = "=R[-2]C+R[-1]C/100"
'********** FORMATOWANIE PÓL Dx i X ******************
rb = "B" + Trim(Str(liczba + 6)) + ":B" + Trim(Str(liczba + 7))
Range(rb).Select
Selection.NumberFormat = "0.00"
Selection.HorizontalAlignment = xlRight
'*********** OBLICZENIE V **********************
Range("D4").Select: ActiveCell.FormulaR1C1 = "=(dx-RC[-1])"
rd = "D4:D" + Trim(Str(liczba + 3))
Range("D4").Select
Selection.AutoFill Destination:=Range(rd), Type:=xlFillDefault
'************ OBLICZENIE SUMY V **************************
Range("D" + Trim(Str(liczba + 4))).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" + lis + "]C:R[-1]C)"
'*********** OBLICZENIE VV *************************
Range("E4").Select: ActiveCell.FormulaR1C1 = "=RC[-1]^2"
re = "E4:E" + Trim(Str(liczba + 3))
Range("E4").Select
Selection.AutoFill Destination:=Range(re), Type:=xlFillDefault
'************ OBLICZENIE SUMY VV **********************
Range("E" + Trim(Str(liczba + 4))).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" + Trim(Str(liczba)) + "]C:R[-1]C)"
'************ FORMATOWANIE PÓL V I VV ******************
rb = "D4:E" + Trim(Str(liczba + 4))
Range(rb).Select
Selection.NumberFormat = "0.00"
Selection.HorizontalAlignment = xlRight
'************ WSTAWIANIE NAPISÓW m= i mx = ***********************
rd = "D" + Trim(Str(liczba + 6))
Range(rd).Select: ActiveCell.FormulaR1C1 = "m ="
Selection.HorizontalAlignment = xlRight
rd = "D" + Trim(Str(liczba + 7))
Range(rd).Select
ActiveCell.FormulaR1C1 = "mx ="
ActiveCell.Characters(Start:=2, Length:=1).Font.Subscript = True
Selection.HorizontalAlignment = xlRight
'************ OBLICZANIE WARTOŚCI m= i mx = ***********************
re = "E" + Trim(Str(liczba + 6))
Range(re).Select
ActiveCell.FormulaR1C1 = "=SQRT(R[-2]C/" + Trim(Str(liczba - 1)) + ")"
re = "E" + Trim(Str(liczba + 7))
Range(re).Select: ActiveCell.FormulaR1C1 = "=R[-1]C/SQRT(" + lis + ")"
'************ FORMATOWANIE PÓL m i mx ******************************
rb = "E" + Trim(Str(liczba + 6)) + ":E" + Trim(Str(liczba + 7))
Range(rb).Select
Selection.NumberFormat = "0.0"
'**************** RYSOWANIE RAMEK *********************************
r2 = "A3:E" + ls
Range(r2).Select
With Selection
.Borders(xlEdgeLeft).Weight = xlThick
.Borders(xlEdgeTop).Weight = xlThick
.Borders(xlEdgeBottom).Weight = xlThick
.Borders(xlEdgeRight).Weight = xlThick
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
End With
'******** ZAZNACZENIE PÓL NIECHRONIONYCH **********
rb = "B4:B" + ls
Range(rb).Select
Selection.Locked = False
Selection.FormulaHidden = False
Selection.Interior.ColorIndex = 27
'*********** WŁĄCZENIE OCHRONY ARKUSZA **************
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
Range("B4").Select
End Sub