简体   繁体   中英

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. The data is provided by a dynamic table. I created a chart sheet and inserted the regional map chart into it. 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. For example: "A4293:A4295,BJ4293:BJ4295"

The error 1004 comes up in the last line.

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”

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. I have two "Filled Mpa" type charts, one embedded in the worksheet, the other moved to a chart sheet. The maps initially draw their data from 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:

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

Note: Chart Sheet does not contains ChartsObject collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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