热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ExcelVBA:将散点图作为对象添加到工作表-ExcelVBA:Addascattercharttoworksheetasobject

Iamlookingtocreateascatterchartthat,onbuttonpress,createsascatterchartinS

I am looking to create a scatter chart that, on button press, creates a scatter chart in Sheet 1 and uses A2:A11 as the x values and B2:B11 as the y values.

我打算创建一个散点图,按下按钮,在Sheet 1中创建一个散点图,并使用A2:A11作为x值,B2:B11作为y值。

Using code at the bottom allows me to create a scatter chart based off of the values in A1:B3 (got this from here). Its close, but not exactly what I'm looking for. How can I tweak this to suit my needs?

使用底部的代码允许我根据A1:B3中的值创建散点图(从此处获取)。它很接近,但不完全是我正在寻找的。我怎样才能调整它以满足我的需求?

I got it set up now so the chart is made, based on the values I want, but I can not get it to appear as an object in Sheet 1. How do I do this? .Location xlLocationAsObject doesn't seem to work.

我现在设置它,所以根据我想要的值制作图表,但是我不能让它在Sheet 1中作为对象出现。我该怎么做? .Location xlLocationAsObject似乎不起作用。

Private Sub chartButton_Click()
    ActiveWorkbook.Charts.Add
    With ActiveWorkbook.ActiveChart
        'Data?
        .ChartType = xlXYScatter
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = "=""Scatter Chart"""
        .SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$11"
        .SeriesCollection(1).Values = "=Sheet1!$B$2:$B$11"

        'Location
        'DON'T KNOW WHAT TO PUT HERE
        '.location xlLocationAsObject doesn't work!

        'Titles
        .HasTitle = True
        .ChartTitle.Characters.Text = "Scatter Chart"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X values"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y values"
        .Axes(xlCategory).HasMajorGridlines = True

        'Formatting
        .Axes(xlCategory).HasMinorGridlines = False
        .Axes(xlValue).HasMajorGridlines = True
        .Axes(xlValue).HasMinorGridlines = False
        .HasLegend = False

    End With
End Sub

2 个解决方案

#1


8  

Your code as written adds a chart as a Chart Sheet, not as a chart on a Worksheet

您编写的代码将图表添加为图表工作表,而不是工作表上的图表

Try this:

尝试这个:

Replace

更换

ActiveWorkbook.Charts.Add
With ActiveWorkbook.ActiveChart

with

Dim sh As Worksheet
Dim chrt As Chart

Set sh = ActiveWorkbook.Worksheets("Sheet1")
Set chrt = sh.Shapes.AddChart.Chart
With chrt

Then you can control its position and size with

然后你可以控制它的位置和大小

    .ChartArea.Left
    .ChartArea.Top 
    .ChartArea.Height
    .ChartArea.Width 

#2


0  

When you use Chart.Location, you need to specify the location:

使用Chart.Location时,需要指定位置:

.Location Where:=xlLocationAsObject, Name:="Sheet1"

推荐阅读
author-avatar
大耍酷的微博Katharine
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有