众所周知,excel软件为使用者提供了添加迷你图的功能(从Excel 2010开始的一种新增功能)。迷你图包含了折线图,柱状图和盈亏图。


在前面的文章中我有介绍过相关内容,以及如何批量制作迷你饼图的方法代码。


这里要分享的是如何制作仿迷你折线图并添加横坐标轴。我们先看一下如何制作迷你折线图。



但是如果我想显示正常的横坐标轴怎么办?经过一番资料查找,我发现excel本身自带的迷你图并不具备显示横坐标轴标签的功能,只能提供一条水平线。


然而为迷你折线图添加横坐标轴是我在工作中遇到的实际问题。因为我不仅想看到趋势,也想知道时间节点。



于是,在对制作迷你饼图的代码基础上稍加修改后,完美解决了我的工作需求。

Sub 迷你折线图() Dim Rg, Rgrow As Range Dim cht As Chart Set Rg = Selection For i = 1 To Rg.Rows.Count Set Rgrow = Rg.Rows(i) Set pcl = Rgrow.Cells(1, 1).Offset(0, Rgrow.Columns.Count) chtype = xlLineMarkers Set cht = ActiveSheet.ChartObjects.Add(Left:=pcl.Left, Width:=pcl.Width, Top:=pcl.Top, Height:=pcl.Height).Chart On Error Resume Next With cht .HasTitle = False .HasLegend = False .ChartType = chtype .SetSourceData Source:=Rgrow .ChartArea.Border.LineStyle = xlNone .PlotArea.Border.LineStyle = xlNone .ChartArea.Fill.Visible = False .PlotArea.Fill.Visible = False .SetElement (msoElementPrimaryValueGridLinesNone) .SetElement (msoElementPrimaryValueAxisNone) End With cht.Axes(xlCategory).Select ActiveChart.FullSeriesCollection(1).XValues = "=Sheet2!$b$1:$g$1" '横坐标轴标签内容位置 ActiveChart.Axes(xlCategory).TickLabels.Font.Size = 8 '横坐标轴标签字体大小 cht.PlotArea.Select Selection.Top = 0 Selection.Height = pcl.Height Selection.Left = 0 Selection.Width = pcl.Width Next i End Sub