Last active
February 15, 2019 11:58
-
-
Save chenxizhang/3bc5e940f59f9e30d13cb88e3a6c8a6a to your computer and use it in GitHub Desktop.
Report Generator VBA Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub ReportGeneration() | |
' 这个方法用来生成销售数据,并且基于这些数据生成一个柱状图表 | |
' 作者:陈希章 | |
Dim sh As Worksheet | |
Set sh = ThisWorkbook.Worksheets.Add | |
sh.Activate | |
Dim rng As Range | |
Set rng = sh.Cells(1, 1) | |
rng.Value = "Quarterly Sales Report" | |
rng.Font.Name = "Century" | |
rng.Font.Size = 26 | |
rng.Resize(1, 5).Merge | |
rng.HorizontalAlignment = xlCenter | |
Dim headerRow As Range | |
Set headerRow = rng.Offset(1).Resize(, 5) | |
headerRow.Value = [{"Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"}] | |
headerRow.Font.Bold = True | |
Dim dataRng As Range | |
Set dataRng = rng.Offset(2).Resize(6, 5) | |
dataRng.Value = [{"Frames", 5000, 7000, 6544, 4377;"Saddles", 400, 323, 276, 651;"Brake levers", 12000, 8766, 8456, 9812;"Chains", 1550, 1088, 692, 853;"Mirrors", 225, 600, 923, 544;"Spokes", 6005, 7634, 4589, 8765}] | |
dataRng.Columns.AutoFit | |
sh.Range("A1").ColumnWidth = 20 | |
With sh.Range("B1:E8") | |
.ColumnWidth = 15 | |
.HorizontalAlignment = xlCenter | |
End With | |
Dim co As ChartObject | |
Set co = sh.ChartObjects().Add(0, sh.Range("A1:A8").Height + 5, sh.Range("A1:E1").Width, 200) | |
co.chart.ChartWizard Source:=sh.Range("A2:E8"), Title:="Quarterly sales chart", Gallery:=xlColumnClustered | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
花了好大力气才知道,教程里的代码地址需要翻墙才能打开!!!
汗!!