excel2007VBA绘图2

时间:2023-03-09 04:13:04
excel2007VBA绘图2

 '--------------------------------------------------------Module1--------------------------------------------------------
Sub Macro1()
ActiveSheet.Range("B1:B40").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$B$40")
With ActiveChart
.HasTitle = True '为图表设置标题
With .ChartTitle
.Text = "交会图版"
.Font.Name = "宋体"
.Font.Size =
.Font.ColorIndex =
.Top =
.Left =
End With
With .Axes(xlCategory) '为图表设置横坐标
.HasTitle = True
.AxisTitle.Text = "统计含量"
.AxisTitle.Font.Name = "宋体"
.AxisTitle.Font.Size =
.AxisTitle.Font.Bold = False
.AxisTitle.Font.ColorIndex =
.MinimumScale = Cells(, ) - Cells(, ) * 0.2
.MaximumScale = Cells(, ) + Cells(, ) * 0.2
End With With .Axes(xlValue) '为图表设置纵坐标
.HasTitle = True
.AxisTitle.Text = "百分数"
.AxisTitle.Font.Name = "宋体"
.AxisTitle.Font.Size =
.AxisTitle.Font.Bold = False
.AxisTitle.Font.ColorIndex =
' i = Excel.Application.WorksheetFunction.Match("203", f, 0)
.MinimumScale = Excel.Application.WorksheetFunction.Min(Range("B:B")) '**zbl**范围要加一个Range才不会出错,下面的不行
.MaximumScale = Excel.Application.WorksheetFunction.Max(Range("B:B"))
' .MinimumScale = Excel.Application.WorksheetFunction.Min("B:B")
' .MaximumScale = Excel.Application.WorksheetFunction.Max("B:B")
' .MinimumScale = Application.WorksheetFunction.Min("b1:b40")
' .MaximumScale = Application.WorksheetFunction.Max("b4:b40")
'Sub UseFunction()
' Dim myRange As Range
' Set myRange = Worksheets("Sheet1").Range("A1:C10")
' answer = Application.WorksheetFunction.Max搜索(myRange)
' MsgBox answer
'End Sub
End With
.HasLegend = True '为图表设置图例
With .Legend
.Position = xlLegendPositionRight
.Font.ColorIndex =
.Font.Bold = True
End With
End With End Sub

多个系列:

excel2007VBA绘图2

参考:http://club.excelhome.net/thread-1199400-1-1.html

excel2007VBA绘图2

 '--------------------------------------------------------Module1--------------------------------------------------------
Sub Macro1()
ActiveSheet.Range("B1:C40").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth '平滑线散点图
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$C$40")
With ActiveChart
.HasTitle = True '为图表设置标题
.SeriesCollection().Name = "sin" '为图例设置名称
.SeriesCollection().Name = "cos" '.SeriesCollection(i).Name=Worksheets("Sheet1").Range("A" & i)
With .ChartTitle
.Text = "正弦余弦图"
.Font.Name = "宋体"
.Font.Size =
.Font.ColorIndex =
.Top =
.Left =
End With
With .Axes(xlCategory) '为图表设置横坐标
.HasTitle = True
.AxisTitle.Text = "统计含量"
.AxisTitle.Font.Name = "宋体"
.AxisTitle.Font.Size =
.AxisTitle.Font.Bold = False
.AxisTitle.Font.ColorIndex =
.MinimumScale = Cells(, ) - Cells(, ) * 0.2
.MaximumScale = Cells(, ) + Cells(, ) * 0.2
End With With .Axes(xlValue) '为图表设置纵坐标
.HasTitle = True
.AxisTitle.Text = "百分数"
.AxisTitle.Font.Name = "宋体"
.AxisTitle.Font.Size =
.AxisTitle.Font.Bold = False
.AxisTitle.Font.ColorIndex =
' i = Excel.Application.WorksheetFunction.Match("203", f, 0)
.MinimumScale = Excel.Application.WorksheetFunction.Min(Range("B:B")) '**zbl**范围要加一个Range才不会出错,下面的不行
.MaximumScale = Excel.Application.WorksheetFunction.Max(Range("B:B"))
' .MinimumScale = Excel.Application.WorksheetFunction.Min("B:B")
' .MaximumScale = Excel.Application.WorksheetFunction.Max("B:B")
' .MinimumScale = Application.WorksheetFunction.Min("b1:b40")
' .MaximumScale = Application.WorksheetFunction.Max("b4:b40")
'Sub UseFunction()
' Dim myRange As Range
' Set myRange = Worksheets("Sheet1").Range("A1:C10")
' answer = Application.WorksheetFunction.Max搜索(myRange)
' MsgBox answer
'End Sub
End With
.HasLegend = True '为图表设置图例
With .Legend
.Position = xlLegendPositionRight
.Font.ColorIndex =
.Font.Bold = True
End With End With End Sub

设置网格线:

excel2007VBA绘图2

 '--------------------------------------------------------Module1--------------------------------------------------------
Sub Macro1()
ActiveSheet.Range("B1:C40").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth '平滑线散点图
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$C$40")
ActiveWindow.GridlineColor = RGB(, , ) '窗体excel网格周围的线成红色了
ActiveWindow.DisplayGridlines = False
With ActiveChart
.HasTitle = True '为图表设置标题
.SeriesCollection().Name = "sin" '为图例设置名称
.SeriesCollection().Name = "cos" '.SeriesCollection(i).Name=Worksheets("Sheet1").Range("A" & i)
With .ChartTitle
.Text = "正弦余弦图"
.Font.Name = "宋体"
.Font.Size =
.Font.ColorIndex =
.Top =
.Left =
End With
With .Axes(xlCategory) '为图表设置横坐标
.HasTitle = True
.AxisTitle.Text = "统计含量"
.AxisTitle.Font.Name = "宋体"
.AxisTitle.Font.Size =
.AxisTitle.Font.Bold = False
.AxisTitle.Font.ColorIndex =
.MinimumScale = Cells(, ) - Cells(, ) * 0.2
.MaximumScale = Cells(, ) + Cells(, ) * 0.2
End With With .Axes(xlValue) '为图表设置纵坐标
.HasTitle = True
.AxisTitle.Text = "百分数"
.AxisTitle.Font.Name = "宋体"
.AxisTitle.Font.Size =
.AxisTitle.Font.Bold = False
.AxisTitle.Font.ColorIndex =
' i = Excel.Application.WorksheetFunction.Match("203", f, 0)
.MinimumScale = Excel.Application.WorksheetFunction.Min(Range("B:B")) '**zbl**范围要加一个Range才不会出错,下面的不行
.MaximumScale = Excel.Application.WorksheetFunction.Max(Range("B:B"))
' .MinimumScale = Excel.Application.WorksheetFunction.Min("B:B")
' .MaximumScale = Excel.Application.WorksheetFunction.Max("B:B")
' .MinimumScale = Application.WorksheetFunction.Min("b1:b40")
' .MaximumScale = Application.WorksheetFunction.Max("b4:b40")
'Sub UseFunction()
' Dim myRange As Range
' Set myRange = Worksheets("Sheet1").Range("A1:C10")
' answer = Application.WorksheetFunction.Max搜索(myRange)
' MsgBox answer
'End Sub
End With
.HasLegend = True '为图表设置图例
With .Legend
.Position = xlLegendPositionRight
.Font.ColorIndex =
.Font.Bold = True
End With
Range("c1:d2").Borders.LineStyle = xlContinuous '区域全部设置线
Range("c1:d2").Borders.LineStyle = xlNone '取消单元格线’
Range("d4").Borders(xlEdgeTop).LineStyle = xlContinuous '只设置单元格d3顶线 http://www.excelpx.com/thread-294531-1-1.html With Range("a1:b2").Borders
.LineStyle = xlContinuous '设置边框线
.Weight = xlThick '设置边框线为粗线
End With End With End Sub

说明

每个 ChartObject 对象都代表一个嵌入图表。ChartObject 对象充当 Chart 对象的容器。ChartObject
对象的属性和方法控制工作表上嵌入图表的外观和大小。ChartObjects 集合

示例

使用 ChartObjects 方法返回 ChartObjects
集合。以下示例删除名为“Sheet1”的工作表上的所有嵌入图表。

Visual Basic for Applications
Worksheets("sheet1").ChartObjects.Delete

不能使用 ChartObjects 集合来调用以下属性和方法:

与早期版本不同,ChartObjects 集合现在可以读取有关高度、宽度、左对齐和顶对齐的属性。

使用 Add 方法可创建一个新的空嵌入图表并将它添加到集合中。使用 ChartWizard 方法可添加数据并设置新图表的格式。以下示例创建一个新嵌入图表,然后以折线图形式添加单元格 A1:A20 中的数据。

Visual Basic for Applications
Dim ch As ChartObject
Set ch = Worksheets("sheet1").ChartObjects.Add(100, 30, 400, 250)
ch.Chart.ChartWizard source:=Worksheets("sheet1").Range("a1:a20"), _
gallery:=xlLine, title:="New Chart"

使用 ChartObjects(index)(其中 index 是嵌入图表的索引号或名称)可以返回单个对象。以下示例设置名为“Sheet1”的工作表上嵌入图表 Chart 1 中的图表区图案。

Visual Basic for Applications
Worksheets("Sheet1").ChartObjects(1).Chart. _
CChartObjecthartArea.Format.Fill.Pattern = msoPatternLightDownwardDiagonal
 Dim ch As ChartObject
Set ch = Worksheets("sheet1").ChartObjects.Add(, , , )
ch.Chart.ChartWizard source:=Worksheets("sheet1").Range("a1:a20"), _
gallery:=xlLine, title:="New Chart"