Program pyta na początku o liczbę wartości do wyrównania: Następnie program przygotowuje formularz do obliczenia średniej arytmetycznej, oraz oceny jej dokładności dostosowany do podanej liczby spostrzeŝeń. Wzory i tok obliczeń zostały omówione wcześniej przy ręcznym tworzeniu formularza dla średniej arytmetycznej. Sub srednia()
'**************** WYCZYSZCZENIE ZAWARTOŚCI ARKUSZA ***** ActiveSheet.Unprotect Range("A2:F30").Select Selection.Delete Shift:=xlUp '**************** WSTAWIENIE TYTUŁU ******************* Range("B1").Select Selection.Font.Italic = True ActiveCell.FormulaR1C1 = "Obliczenie średniej arytmetycznej" '******************* PYTANIE O LICZBĘ POMIARÓW ***************** Range("C2").Select liczba = InputBox("Podaj liczbe wartości do usrednienia") 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 ActiveCell.FormulaR1C1 = "xmin=" With ActiveCell.Characters(Start:=2, Length:=3).Font.Subscript = True '*********** WSTAWIANIE NAPISU DELTA X ************** r4 = "A" + Trim(Str(liczba + 6)) Range(r4).Select ActiveCell.FormulaR1C1 = "Dx=" With ActiveCell.Characters(Start:=1, Length:=1).Font.Name = "Symbol"
'********* WSTAWIANIE NAPISU X= ********************** r5 = "A" + Trim(Str(liczba + 7)) Range(r5).Select ActiveCell.FormulaR1C1 = "X=" '********** 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("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)) 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)) 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)) '*********** 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)) '************ WSTAWIANIE NAPISÓW m= i mx = *********************** rd = "D" + Trim(Str(liczba + 6)) Range(rd).Select: ActiveCell.FormulaR1C1 = "m =" rd = "D" + Trim(Str(liczba + 7)) Range(rd).Select ActiveCell.FormulaR1C1 = "mx =" ActiveCell.Characters(Start:=2, Length:=1).Font.Subscript = True '************ 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)) 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 '******** ZAZNACZENIE PÓL NIECHRONIONYCH ********** rb = "B4:B" + ls 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 Po wykonaniu programu otrzymujemy gotowy formularz, do którego dane wpisujemy w polach zaznaczonych Ŝółtym kolorem.interior.colorindex = 27 (pozostałe pola są chronione).