gpt4 book ai didi

excel - 无法成功移动箱线图

转载 作者:行者123 更新时间:2023-12-03 03:08:58 24 4
gpt4 key购买 nike

我一直在网上浏览,并收集了一些代码,这些代码将创建一个箱线图并将其移动到另一个工作表。但是,以下代码能够创建箱线图,但无法将其移动到新工作表,并且当尝试时,会弹出此错误

Run-time error '1004' Method 'Location' of object '_Chart' failed

在这条线上

Set c = c.Location(where:=xlLocationAsNewSheet, Name:="newChartSheetName")

当我尝试再次运行它时,我的 Excel 文件将自行关闭而不保存。我尝试在我想要的工作表中创建箱线图,但数据来自另一个工作表,这使得图表为空,这就是我更改为此方法的原因。以下是我目前对这个问题的尝试。感谢所提供的任何帮助。

Sub test_boxplot()
Dim chart_title As String
Dim RngToCover As Range
Dim ChtOb As ChartObject, c As Chart

With Sheets("Data")
.Select
.Range("E6:E425").Select ' I understand this is not an efficient way to go about it.

chart_title = .Range("E2")

.Shapes.AddChart2(408, xlBoxwhisker).Select
ActiveChart.ChartTitle.Text = chart_title
End With

With Sheets("Graphs")
Set RngToCover = .Range("L6:O23")
End With

Set ChtOb = ActiveChart.Parent
Set c = ChtOb.Chart

Set c = c.Location(where:=xlLocationAsNewSheet, Name:="newChartSheetName") 'Error here
c.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)

With ChtOb
.Height = RngToCover.Height ' resize
.Width = RngToCover.Width ' resize
.Top = RngToCover.Top ' reposition
.Left = RngToCover.Left ' reposition
End With

End Sub

最佳答案

  1. 如果我没记错的话,.Location where:=xlLocationAsNewSheet 会给出 BoxPlot 错误,但成功移动图表。您可以使用 On Error Resume Next |出现错误时,转到 0 来解决此错误。
  2. 还有一个问题。名称成功将更改为 newChartSheetName,但不会反射(reflect)在 VBE 项目属性中。它将显示为 ChartXXX,如下图所示。仅当保存、关闭并重新打开文件后,该名称才会显示在 VBE 中。不过,该名称确实会在属性窗口中更新。找不到任何 MS 知识库来支持我的上述说法。您可以自己尝试和测试。

    enter image description here

  3. 如果您尝试创建柱状图,然后将其作为图表工作表移动,它会允许您使用,但不会让您使用 .ChartType 来更改它到xlBoxwhisker。它会给您指定的维度对于当前图表类型无效错误。这通常发生在 Excel 无法创建/转换为复杂图表类型的情况下。

  4. 有一个替代方案,但这确实是一个错误。 Charts.Add 有第四个参数,即 Type:=,您可以在其中指定 xlBoxwhisker,但会失败并出现运行时错误 1004。这是一个错误从 Excel 2007 开始。您也不能将其用于任何其他类型。

  5. 同时避免使用.Select/Activechart。使用物体并与它们一起工作。这将使您的生活更轻松。

这是一个例子

Option Explicit

Sub Sample()
Dim ws As Worksheet, wsChrt As Chart
Dim objChart As ChartObject
Dim chrt As Chart
Dim shp As Shape

'~~> Change this to the relevant sheet
Set ws = Sheet1

'~~> Delete existing chart if any
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("test").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'~~> Create new chart and move
With ws
Set shp = .Shapes.AddChart2(408, xlBoxwhisker, 200, 100, 350, 200, True) '
Set objChart = .ChartObjects(shp.Name)
Set chrt = objChart.Chart

With chrt
'~~> Set your source data here
.SetSourceData Source:=ws.Range("E6:E11")

On Error Resume Next
.Location where:=xlLocationAsNewSheet, Name:="test"
Set wsChrt = ThisWorkbook.Sheets("test")
On Error GoTo 0
End With

If Not wsChrt Is Nothing Then
MsgBox "Chart Moved"
With wsChrt
'
'~~> Do what you want with the chart
'
End With
Else
MsgBox "Error in creating a chart"
End If
End With
End Sub

关于excel - 无法成功移动箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59316912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com