Chcete sledovať denný príjem energie v strave? Zostavte si program v Exceli! Natypujte do jednotlivých hárkov v Exceli energetickú hodnotu potravín, ako aj obsah bielkovín, uhlohydrátov a tukov. Hárky pomenujte podľa druhu potravín, napr.:
Obr.1 ilustruje zápis potrebných parametrov v jednotlivých hárkoch. Všetky hodnoty platia na 100 g potraviny. Ak máme túto sysifovskú prácu za sebou, vložíme v editore Visual Basic-u modul a do modulu zapíšeme nasledujúci program (všimnime si, že premenná po je deklarovaná pomocou kľúčového slova public na začiatku modulu a to znamená, že je prístupná zo všetkých procedúr a vo všetkých moduloch, teda v celom projekte.
Public po As Integer
Sub Start()
On Error GoTo kon
ActiveSheet.ChartObjects(1).Activate
ActiveChart.ChartArea.Select
ActiveWindow.Visible = False
Selection.Delete
kon:
Application.ScreenUpdating = False
Dim m As Integer, k As Integer
m = 50
For k = 2 To m
' Zmaže predcházajúce zápisy na hárku
Cells(k, 1) = ""
Cells(k, 2) = ""
Cells(k, 3) = ""
Next k
For k = 3 To m
' v bunke E2, F2, G2, H2 ponechá vzorec!
Cells(k, 5) = ""
Cells(k, 6) = ""
Cells(k, 7) = ""
Cells(k, 8) = ""
Next k
UserForm1.Show
End Sub
RowSource Mäso!a2:e25
V ListBox-e máme 5 stĺpcov:
Private Sub Začiatok_Click()
' vložíme do hárku Výber
Application.ScreenUpdating = False
Call Start
End Sub
Sub Súčet_Click()
' vložíme do hárku Výber
Application.ScreenUpdating = False
' Spočíta KiloCalórie, bielkoviny, tuky a uhlohydráty
Dim k As Integer
' Do nasledujúcich premenných
' sa postupne vložia súčty
ss = 0# 'kCal
ss1 = 0# 'bielkoviny [g]
ss2 = 0# 'tuky [g]
ss3 = 0# 'uhlohydráty [g]
Range("B1").Select
ActiveCell.CurrentRegion.Select
pr = Selection.Areas(1).Rows.Count
' pr je počet zvolených potravín
Set SourceRange = Worksheets(1).Range(Cells(2, 5), Cells(2, 5))
' v riadku 2 stĺpca 5,6,7 a 8 v hárku Výber sú
' zadané vzorce (výpočet kCal, množstva bielkovín,
' tukov a uhlohydrátov
Set fillRange = Worksheets(1).Range(Cells(2, 5), Cells(pr, 5))
SourceRange.AutoFill Destination:=fillRange
Set SourceRange = Worksheets(1).Range(Cells(2, 6), Cells(2, 6))
Set fillRange = Worksheets(1).Range(Cells(2, 6), Cells(pr, 6))
SourceRange.AutoFill Destination:=fillRange
Set SourceRange = Worksheets(1).Range(Cells(2, 7), Cells(2, 7))
Set fillRange = Worksheets(1).Range(Cells(2, 7), Cells(pr, 7))
SourceRange.AutoFill Destination:=fillRange
Set SourceRange = Worksheets(1).Range(Cells(2, 8), Cells(2, 8))
Set fillRange = Worksheets(1).Range(Cells(2, 8), Cells(pr, 8))
SourceRange.AutoFill Destination:=fillRange
For k = 2 To pr
ss = ss + Cells(k, 5)
ss1 = ss1 + Cells(k, 6)
ss2 = ss2 + Cells(k, 7)
ss3 = ss3 + Cells(k, 8)
Next k
Range(Cells(2, 5), Cells(30, 8)).Select
Selection.NumberFormat = "0.0"
Selection.Font.Bold = True
With Selection.Font
.Name = "Arial"
.Size = 10
.ColorIndex = xlAutomatic
End With
po = pr + 1 ' tu nadobúda premenná
' po hodnotu, ktorá bude potrebná pri
' zostrojení koláčového grafu
Cells(po, 5).Value = ss
Cells(po, 6).Value = ss1
Cells(po, 7).Value = ss2
Cells(po, 8).Value = ss3
Range(Cells(po, 5), Cells(po, 8)).Select
With Selection.Font
.Name = "Arial"
.Size = 16
End With
End Sub
Sub Grf_Click()
' vložíme do hárku Výber
pa = po - 1
Dim myUnion As Range
Application.ScreenUpdating = False
Worksheets(1).Activate
Set myUnion = Union(Cells(1, 6), _
Cells(1, 7), Cells(1, 8), Cells(po, 6), _
Cells(po, 7), Cells(po, 8))
myUnion.Select
adr = myUnion.Address
Charts.Add
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Sheets("Výber").Range(adr), _
PlotBy:=xlRows
ActiveChart.Location Where:=xlLocationAsObject, Name:="Výber"
ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowPercent
Worksheets(1).Activate
ActiveSheet.ChartObjects(1).Activate
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Points(2).Select
With Selection.Interior
.ColorIndex = 3
.PatternColorIndex = 1
.Pattern = 1
End With
ActiveChart.SeriesCollection(1).DataLabels.Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Tučné"
.Size = 14
End With
ActiveChart.Legend.Font.Size = 10
With Selection.Font
.Name = "Arial"
.FontStyle = "Tučné"
.Size = 10
End With
With ActiveChart
.PlotArea.Interior.ColorIndex = 55
End With
ActiveChart.ChartArea.Select
With Selection.Interior
.ColorIndex = 48
.PatternColorIndex = 1
.Pattern = 1
End With
With ThisWorksheet
Worksheets(1).ChartObjects(1).Width = 245
Worksheets(1).ChartObjects(1).Height = 182
Worksheets(1).ChartObjects(1).Top = 55
Worksheets(1).ChartObjects(1).Left = 454
End With
ActiveChart.Location Where:=xlLocationAsObject, Name:="Výber"
Windows("Kalorie.xls").Activate
End Sub