-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelMakro.bas
197 lines (158 loc) · 6.93 KB
/
ExcelMakro.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
Sub AuswertungErstellen()
Dim wsFragenkatalog As Worksheet
Dim wsHilfstabelle As Worksheet
Dim wsAuswertung As Worksheet
Dim tblFragenkatalog As ListObject
Dim tblAuswertung As ListObject
Dim lastRowHilfstabelle As Long
Dim i As Long, j As Long, k As Long, l As Long
Dim frage As String
Dim antwort As String
Dim gewichtung As Double
Dim antwortKategorie As String
Dim kategorie As String
' Set worksheets
Set wsFragenkatalog = ThisWorkbook.Sheets("Fragenkatalog")
Set wsHilfstabelle = ThisWorkbook.Sheets("Hilfstabelle Antworten")
Set wsAuswertung = ThisWorkbook.Sheets("Auswertung")
' Get the table "Fragenkatalog" and "AuswertungKategorien"
Set tblFragenkatalog = wsFragenkatalog.ListObjects("Fragenkatalog")
Set tblAuswertung = wsAuswertung.ListObjects("AuswertungKategorien")
' Get the last row of Hilfstabelle
lastRowHilfstabelle = wsHilfstabelle.Cells(wsHilfstabelle.Rows.Count, "A").End(xlUp).Row
Debug.Print "Last Row in Hilfstabelle: " & lastRowHilfstabelle
' Check if the rows in Fragenkatalog are found
Debug.Print "Fragenkatalog Rows Count: " & tblFragenkatalog.ListRows.Count
Debug.Print "Auswertung Rows Count: " & tblAuswertung.ListRows.Count
' Initialize Auswertung table
For i = 1 To tblAuswertung.ListRows.Count
Debug.Print "Initializing Auswertung, Row: " & i
For j = 2 To tblAuswertung.ListColumns.Count
tblAuswertung.DataBodyRange(i, j).Value = 0
Next j
Next i
' Loop through each question in Fragenkatalog
For i = 1 To tblFragenkatalog.ListRows.Count
frage = tblFragenkatalog.DataBodyRange(i, 2).Value
gewichtung = tblFragenkatalog.DataBodyRange(i, 3).Value
antwort = tblFragenkatalog.DataBodyRange(i, 5).Value
kategorie = tblFragenkatalog.DataBodyRange(i, 1).Value
Debug.Print "Verarbeite Frage: " & frage & ", Gewichtung: " & gewichtung & ", Antwort: " & antwort & ", Kategorie: " & kategorie
' Find matching answer in Hilfstabelle and get the associated tool
For j = 2 To lastRowHilfstabelle
If wsHilfstabelle.Cells(j, 1).Value = antwort Then
antwortKategorie = wsHilfstabelle.Cells(j, 3).Value
Debug.Print "Gefundene Antwort in Hilfstabelle: " & antwort & ", Kategorie: " & antwortKategorie
' Find the row and column in AuswertungKategorien
For k = 1 To tblAuswertung.ListRows.Count
If tblAuswertung.DataBodyRange(k, 1).Value = kategorie Then
For l = 2 To tblAuswertung.ListColumns.Count
If tblAuswertung.HeaderRowRange(1, l).Value = antwortKategorie Then
Debug.Print "Kategorie gefunden: " & kategorie & ", Spalte: " & l
tblAuswertung.DataBodyRange(k, l).Value = tblAuswertung.DataBodyRange(k, l).Value + gewichtung
Debug.Print "Neuer Wert in Auswertung: " & tblAuswertung.DataBodyRange(k, l).Value
End If
Next l
End If
Next k
End If
Next j
Next i
' Set the worksheet variable to the desired sheet
Set ws = ThisWorkbook.Sheets("Auswertung")
' Activate the worksheet
ws.Activate
' Call the CreateColumnChartOnAuswertung macro
Call CreateColumnChartOnAuswertung
' Call the CreatePieChartOnAuswertung macro
Call CreatePieChartOnAuswertung
' Call the WriteMaxAnsatzToC20 macro
Call WriteMaxAnsatzToC20
If wsAuswertung Is Nothing Then
MsgBox "Das Arbeitsblatt 'Auswertung' wurde nicht gefunden!"
Else
wsAuswertung.Activate
End If
MsgBox "Auswertung wurde erfolgreich erstellt!"
End Sub
Sub CreateColumnChartOnAuswertung()
Dim ws As Worksheet
Dim chartObj As ChartObject
Dim chart As chart
' Set the worksheet to "Auswertung"
Set ws = ThisWorkbook.Sheets("Auswertung")
' Add a new chart object, positioning it next to the table
Set chartObj = ws.ChartObjects.Add(Left:=ws.Cells(1, 5).Left, Width:=500, Top:=ws.Cells(1, 5).Top, Height:=300)
Set chart = chartObj.chart
' Set the data range for the chart
chart.SetSourceData Source:=ws.Range("A1:D6")
' Set the chart type to column chart
chart.ChartType = xlColumnClustered
' Set chart title
chart.HasTitle = True
chart.ChartTitle.Text = "Punktzahl nach Kategorien"
' Set axis titles
chart.Axes(xlCategory, xlPrimary).HasTitle = True
chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Kategorien"
chart.Axes(xlValue, xlPrimary).HasTitle = True
chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Punktzahl"
' Add data labels
Dim series As series
For Each series In chart.SeriesCollection
series.HasDataLabels = True
Next series
' Set the chart legend position
chart.Legend.Position = xlLegendPositionBottom
End Sub
Sub CreatePieChartOnAuswertung()
Dim ws As Worksheet
Dim chartObj As ChartObject
Dim chart As chart
' Set the worksheet to "Auswertung"
Set ws = ThisWorkbook.Sheets("Auswertung")
' Add a new chart object, positioning it below the column chart
Set chartObj = ws.ChartObjects.Add(Left:=ws.Cells(1, 5).Left, Width:=500, Top:=ws.Cells(1, 5).Top + 320, Height:=300)
Set chart = chartObj.chart
' Set the data range for the chart
chart.SetSourceData Source:=ws.Range("A10:B13")
' Set the chart type to pie chart
chart.ChartType = xlPie
' Set chart title
chart.HasTitle = True
chart.ChartTitle.Text = "Gesamtpunktzahl nach Werkzeug"
' Add data labels
Dim series As series
For Each series In chart.SeriesCollection
series.HasDataLabels = True
Next series
' Set the chart legend position
chart.Legend.Position = xlLegendPositionRight
End Sub
Sub WriteMaxAnsatzToC20()
Dim ws As Worksheet
Dim maxValue As Double
Dim maxAnsatz As String
Dim i As Integer
Dim lastRow As Integer
' Set the worksheet to "Auswertung"
Set ws = ThisWorkbook.Sheets("Auswertung")
' Find the last row with data in column B
lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
' Initialize the maximum value
maxValue = -1
' Loop through the rows to find the maximum value
For i = 12 To lastRow
If ws.Cells(i, 2).Value > maxValue Then
maxValue = ws.Cells(i, 2).Value
maxAnsatz = ws.Cells(i, 1).Value
End If
Next i
' Write the max Ansatz to cell C20
ws.Range("C20").Value = maxAnsatz
' Format the cell C20
With ws.Range("C20")
.Font.Color = RGB(255, 0, 0) ' Red color
.Font.Size = 14 ' Font size 20
.Font.Name = "Arial" ' Font Arial
End With
End Sub