1 / 50

Programowanie w języku Visual Basic dla arkusza kalkulacyjnego Excel.

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.

asta
Download Presentation

Programowanie w języku Visual Basic dla arkusza kalkulacyjnego Excel.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programowanie w języku Visual Basic dla arkusza kalkulacyjnego Excel.

  2. 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.

  3. Podstawowe komendy makr w arkuszu kalkulacyjnym

  4. Wstawianie napisów – ActiveCell.FormulaR1C1 = Range("B1").Select ActiveCell.FormulaR1C1 = "Obliczenie średniej arytmetycznej" Range("A3").Select: ActiveCell.FormulaR1C1 = "Nr" Range("B3"). ActiveCell.FormulaR1C1 = "L"

  5. Parametry tekstu:

  6. Zmiana arkusza: Zmiana na Arkusz2 Sheets("Arkusz2").Select

  7. Rysowanie ramek:

  8. Grubości linii gruba (.Weight = xlThick), średnia (.Weight = xlMedium), cienka(.Weight = xlThin),

  9. Zaznaczenie obszaru: Range("A3:F9").Select Rysowanie linii zewnętrznych (Edge): - lewa ramka (xlEdgeLeft) With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThick End With

  10. - 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

  11. - prawa ramka (xlEdgeRight) With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThick End With

  12. 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

  13. 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

  14. 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

  15. 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 "

  16. '******************* 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

  17. '****************** 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

  18. '*************** 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

  19. '********** 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

  20. '*********** 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

  21. '********* 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"

  22. '*********** 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

  23. '********** 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"

  24. '********** 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

  25. '************ 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)"

  26. '************ 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

  27. '************ 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"

  28. '**************** 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

  29. '******** 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

More Related