简体   繁体   English

如何更改图表工作表中图表的源数据 Excel vba

[英]How to change the source data for a chart in a chart sheet Excel vba

all together,全部一起,

I am trying to create a regional map chart that shows data per country.我正在尝试创建一个显示每个国家/地区数据的区域 map 图表。 The data is provided by a dynamic table.数据由动态表提供。 I created a chart sheet and inserted the regional map chart into it.我创建了一个图表表并将区域 map 图表插入其中。 The chart now shows the data for the current countries, but I can't change the data.该图表现在显示当前国家/地区的数据,但我无法更改数据。 The string mapInput is correct and also shows the right Range.字符串 mapInput 是正确的,并且还显示了正确的 Range。 For example: "A4293:A4295,BJ4293:BJ4295"例如:“A4293:A4295,BJ4293:BJ4295”

The error 1004 comes up in the last line.错误 1004 出现在最后一行。

I also tried without "Source:=" And also without "chart"我也试过没有“来源:=”也没有“图表”

    l = Split(ws.Cells(lastRowR, lastColR - 2).Address, "$")(1)

    mapInput = "A" & lastRowR - 1 - cCount & ":A" & lastRowR - 1 & "," & l & 
    lastRowR - 1 - cCount & ":" & l & lastRowR - 1

    Sheets("map").ChartObjects(1).Chart.SetSourceData Source:=ws.Range(mapInput)

Runtime Error 1004 “Application-defined or Object-defined error”运行时错误 1004 “应用程序定义或对象定义错误”

I didn't solve your exact issue, but I set up a working example.我没有解决您的确切问题,但我设置了一个工作示例。 If you correctly specify your chart and your data range, it should work for you.如果您正确指定图表和数据范围,它应该适合您。

I have two ranges on my sheet, one named MapInputNE and the other MapInputSW.我的工作表上有两个范围,一个名为 MapInputNE,另一个名为 MapInputSW。 I have two "Filled Mpa" type charts, one embedded in the worksheet, the other moved to a chart sheet.我有两个“填充 Mpa”类型的图表,一个嵌入在工作表中,另一个移动到图表表中。 The maps initially draw their data from MapInputNE.地图最初从 MapInputNE 中提取数据。

新英格兰地图数据和填充地图

I wrote a general routine to change the data of a chart, regardless of where it is.我编写了一个通用例程来更改图表的数据,无论它在哪里。 Feed it the chart and the data range, and it updates the chart, and also adjusts the projection and mapping level.将图表和数据范围提供给它,它会更新图表,并调整投影和映射级别。

Sub UpdateChartData(cht As Chart, rng As Range)
  With cht
    .SetSourceData Source:=rng
    With .SeriesCollection(1)
      .GeoProjectionType = xlGeoProjectionTypeMercator
      .GeoMappingLevel = xlGeoMappingLevelDataOnly
    End With
  End With
End Sub

Then I made some specific routines to change either the embedded map or the chart sheet map to use either the New England or Southwest data:然后我做了一些特定的例程来更改嵌入式 map 或图表 map 以使用新英格兰或西南数据:

Sub UpdateEmbeddedChartDataNE()
  Dim cht As Chart
  Set cht = Worksheets("Sheet1").ChartObjects(1).Chart
  Dim rng As Range
  Set rng = Worksheets("Sheet1").Range("MapInputNE")
  UpdateChartData cht, rng
End Sub

Sub UpdateEmbeddedChartDataSW()
  Dim cht As Chart
  Set cht = Worksheets("Sheet1").ChartObjects(1).Chart
  Dim rng As Range
  Set rng = Worksheets("Sheet1").Range("MapInputSW")
  UpdateChartData cht, rng
End Sub

Sub UpdateChartSheetDataNE()
  Dim cht As Chart
  Set cht = Charts(1)
  Dim rng As Range
  Set rng = Worksheets("Sheet1").Range("MapInputNE")
  UpdateChartData cht, rng
End Sub

Sub UpdateChartSheetDataSW()
  Dim cht As Chart
  Set cht = Charts(1)
  Dim rng As Range
  Set rng = Worksheets("Sheet1").Range("MapInputSW")
  UpdateChartData cht, rng
End Sub

Each of these routines defines the chart and the data range, and feeds them into the first routine, and the corresponding chart chart switches to the indicated data source.这些例程中的每一个都定义了图表和数据范围,并将它们输入到第一个例程中,相应的图表切换到指定的数据源。

Works well with both maps, though I've only shown the embedded one here.两种地图都适用,尽管我在这里只展示了嵌入的地图。

美国西南部地图数据和填充地图

If you are using Chart Sheet, use following code to set data source:如果您使用的是图表表,请使用以下代码设置数据源:

Dim Cht As Chart
Set Cht = Charts("map")
Cht.SetSourceData ws.Range(mapInput)

Make sure mapInput represent correct data range确保mapInput代表正确的数据范围

Note: Chart Sheet does not contains ChartsObject collection注意:图表工作表不包含ChartsObject集合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM