VBA code

johie

New Member
Joined
May 1, 2018
Messages
2
Hi, I have made a code in VBA and it works :cool::cool::cool:
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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I presume you are supposed to do it yourself not ask someone else to do it for you. What attempt have you made so far?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top