Hi, I have made a code in VBA and it works
but now I really do not know what I've done. Is there any one that can help me explain the VBA CODE below :D
Public Sub GenerateDelayedCharts()
'Assignment 6
Dim lrow As Long
Dim i As Long
Dim crt As Shape
Dim ssh As Worksheet
lrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
Set ssh = ActiveSheet
If lrow < 3 Then
MsgBox "There is too low data to show charts. Function will be terminated."
Exit Sub
End If
'ActiveSheet.Cells(1, 12) = "Study Boards"
ActiveSheet.Cells(1, 13) = "Percentwise distribution"
'ActiveSheet.Cells(1, 15) = "Program Id"
ActiveSheet.Cells(1, 16) = "Percentwise distribution"
'ActiveSheet.Cells(1, 18) = "Faculty"
ActiveSheet.Cells(1, 19) = "Percentwise distribution"
'ActiveSheet.Cells(1, 21) = "Period"
ActiveSheet.Cells(1, 22) = "Number of students"
ssh.Range("F1:F" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 12), Unique:=True
ssh.Range("B1:B" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 15), Unique:=True
ssh.Range("G1:G" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 18), Unique:=True
ssh.Range("D1:D" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 21), Unique:=True
i = 2
While ActiveSheet.Cells(i, 12) <> ""
ssh.Cells(i, 13).Formula = "=CountIf(F:F,L" & i & ")/Count(A2:A" & Rows.Count & ")"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("M2:M" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$L$2:$L$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("A" & lrow + 1).Left
.Top = Range("A" & lrow + 1).Top
.Width = Range("A1:E1").Width
End With
i = 2
While ActiveSheet.Cells(i, 15) <> ""
ssh.Cells(i, 16).Formula = "=CountIf(B:B,O" & i & ")/Count(A2:A" & Rows.Count & ")"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("P1:P" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$O$2:$O$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("F" & lrow + 1).Left
.Top = Range("F" & lrow + 1).Top
End With
i = 2
While ActiveSheet.Cells(i, 18) <> ""
ActiveSheet.Cells(i, 19).Formula = "=CountIf(G:G,R" & i & ")/Count(A:A)"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("S1:S" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$R$2:$R$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("A" & lrow + 20).Left
.Top = Range("A" & lrow + 20).Top
.Width = Range("A1:E1").Width
End With
i = 2
While ActiveSheet.Cells(i, 21) <> ""
ActiveSheet.Cells(i, 22).Formula = "=CountIf(D:D,U" & i & ")"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("V1:V" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$U$2:$U$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("F" & lrow + 20).Left
.Top = Range("F" & lrow + 20).Top
End With
End Sub
but now I really do not know what I've done. Is there any one that can help me explain the VBA CODE below :D
Public Sub GenerateDelayedCharts()
'Assignment 6
Dim lrow As Long
Dim i As Long
Dim crt As Shape
Dim ssh As Worksheet
lrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
Set ssh = ActiveSheet
If lrow < 3 Then
MsgBox "There is too low data to show charts. Function will be terminated."
Exit Sub
End If
'ActiveSheet.Cells(1, 12) = "Study Boards"
ActiveSheet.Cells(1, 13) = "Percentwise distribution"
'ActiveSheet.Cells(1, 15) = "Program Id"
ActiveSheet.Cells(1, 16) = "Percentwise distribution"
'ActiveSheet.Cells(1, 18) = "Faculty"
ActiveSheet.Cells(1, 19) = "Percentwise distribution"
'ActiveSheet.Cells(1, 21) = "Period"
ActiveSheet.Cells(1, 22) = "Number of students"
ssh.Range("F1:F" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 12), Unique:=True
ssh.Range("B1:B" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 15), Unique:=True
ssh.Range("G1:G" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 18), Unique:=True
ssh.Range("D1:D" & Rows.Count).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ssh.Cells(1, 21), Unique:=True
i = 2
While ActiveSheet.Cells(i, 12) <> ""
ssh.Cells(i, 13).Formula = "=CountIf(F:F,L" & i & ")/Count(A2:A" & Rows.Count & ")"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("M2:M" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$L$2:$L$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("A" & lrow + 1).Left
.Top = Range("A" & lrow + 1).Top
.Width = Range("A1:E1").Width
End With
i = 2
While ActiveSheet.Cells(i, 15) <> ""
ssh.Cells(i, 16).Formula = "=CountIf(B:B,O" & i & ")/Count(A2:A" & Rows.Count & ")"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("P1:P" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$O$2:$O$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("F" & lrow + 1).Left
.Top = Range("F" & lrow + 1).Top
End With
i = 2
While ActiveSheet.Cells(i, 18) <> ""
ActiveSheet.Cells(i, 19).Formula = "=CountIf(G:G,R" & i & ")/Count(A:A)"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("S1:S" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$R$2:$R$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("A" & lrow + 20).Left
.Top = Range("A" & lrow + 20).Top
.Width = Range("A1:E1").Width
End With
i = 2
While ActiveSheet.Cells(i, 21) <> ""
ActiveSheet.Cells(i, 22).Formula = "=CountIf(D:D,U" & i & ")"
i = i + 1
Wend
Application.Wait 500
Set crt = ActiveSheet.Shapes.AddChart
crt.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("V1:V" & i - 1)
ActiveChart.FullSeriesCollection(1).XValues = "='Delayed students'!$U$2:$U$" & i - 1
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = "The percentwise distribution of students"
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlLegendPositionBottom
With crt
.Left = Range("F" & lrow + 20).Left
.Top = Range("F" & lrow + 20).Top
End With
End Sub