【问题标题】:VBA Getting an error when trying to move a chartVBA 尝试移动图表时出错
【发布时间】:2014-07-24 06:52:19
【问题描述】:

我编写了以下代码,用于在 Excel 工作表中创建图表:

Sub AddChart()

Dim sh As Worksheet
Dim chrteit As Chart

Set sh = ActiveWorkbook.Worksheets("TraceTable")
Set chrteit = sh.Shapes.AddChart.Chart

lastrows = Range("A2").End(xlDown).Row

With chrteit
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = sh.Range(Cells(2, 6), Cells(lastrows, 6))
.SeriesCollection(1).Values = sh.Range(Cells(2, 7), Cells(lastrows, 7))

    Let chrteit.Parent.Name = "EIT"
    .Parent.Height = Range("N2:N14").Height
    .Parent.Width = Range("N2:T2").Width
    .Parent.top = Range("N2").top
    .Parent.Left = Range("N2").Left
    .Parent.Placement = xlFreeFloating

End With

现在运行此代码后,将使用已使用的数据单元创建一个图表。然后我有另一个宏运行,它将操纵所有数据并移动它,所以我绘制了图表以便在数据被操纵之前轻松引用单元格。无论如何,单元格被重新格式化并调整大小,所以图表保持在原来的位置。我要做的就是使用以下命令将图表移回左侧:

Sub MoveChart()

With ActiveWorkbook.Worksheets("TraceTable")
.ChartObjects("EIT").Left = .Range("N2").Left
End With

End Sub

但我收到一条错误消息,提示“找不到具有指定名称的项目”。并突出显示该行:

.ChartObjects("EIT").Left = .Range("N2").Left

我的代码有什么问题?!它以前工作过一次,但我不确定我改变了什么。请帮忙,谢谢!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    使用Chart 对象的ChartArea 属性。见下文:

    Sub MoveChart()
    
        Dim Sh As Worksheet: Set Sh = ThisWorkbook.Sheets("TraceTable")
        Dim Shp As Chart
    
        With Sh
            Set Shp = .ChartObjects("EIT").Chart
            Shp.ChartArea.Left = .Range("N2").Left
        End With
    
    End Sub
    

    在任何情况下,您分离的宏都可以总结为一个宏,如下所示。

    Sub FullRun()
    
        Dim TSht As Worksheet
        Dim LCol As Long, LRow As Long
        Dim RoundOffR As Range, RoundOffC As Range
        Dim ShotR As Range, ShotC As Range
        Dim Cht As Chart
        Dim DivideR As Range, DivideC As Range
    
        Set TSht = ActiveWorkbook.Sheets("TraceTable")
    
        With TSht
            ' Get boundaries.
            LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            ' Get top row and fill it with color.
            With .Cells(1, 1).Resize(1, LCol)
                .Interior.Color = 14136213
                .Font.Bold = True
            End With
            ' Use Union for a cleaner delete.
            ' Columns to delete are Date, Little, Deviation, F, G
            Union(.Range("D:D"), .Range("F:F"), .Range("H:H"), .Range("O:P")).Delete
            ' Initialize the range to round off.
            Set RoundOffR = .Range("E2:K" & LRow)
            RoundOffR.NumberFormat = "0"
        End With
    
        ' Round the values. If it's from E column, change from "0"
        ' format to "0.0000" format.
        For Each RoundOffC In RoundOffR
            RoundOffC.Value = Application.Round(RoundOffC.Value, 0)
            If RoundOffC.Column = 11 Then
                RoundOffC.NumberFormat = "0.0000"
            End If
        Next
    
        ' Insert new column, distribute some values
        With TSht
            With .Range("C1")
                .EntireColumn.Insert
                .Value = "Sample"
            End With
            .Range("E1").Value = "Type"
            Set ShotR = .Range("D2:D" & LRow)
        End With
    
        For Each ShotC In ShotR
            With ShotC
                ' Get the last two digits of D and put in E.
                .Offset(0, 1).Value = Right(.Value, 2)
                ' Get the 10th position in D, get next two characters, and put in C.
                .Offset(0, -1).Value = Right(.Value, 2)
                ' Get the last two digits in B, and replace D.
                .Value = Right(.Offset(0, -2).Value, 2)
            End With
        Next
    
        ' Adjust formatting and create chart.
        With TSht
            .Cells.HorizontalAlignment = xlCenter
            .Columns.AutoFit
            .Rows.AutoFit
            Set Cht = .Shapes.AddChart.Chart
        End With
    
        ' Manipulate chart.
        With Cht
            .Parent.Name = "EIT"
            .ChartType = xlXYScatter
            .SeriesCollection.NewSeries
            With .SeriesCollection(1)
                .XValues = TSht.Range("F2:F" & LRow)
                .Values = TSht.Range("G2:G" & LRow)
            End With
            ' User .ChartArea for size and position.
        End With
    
        ' "Divide" the rows.
        Set DivideR = TSht.Range("B2:B" & LRow)
        For Each DivideC In DivideR
            ' If current cell is not empty and not equal to next cell, insert a row.
            If Not IsEmpty(DivideC) And DivideC.Value <> DivideC.Offset(1, 0).Value Then
                DivideC.Offset(1, 0).EntireRow.Insert
            End If
        Next
    
        ' Add borders.
        TSht.Cells.SpecialCells(xlCellTypeConstants).Borders.LineStyle = xlContinuous
    
        ' Resize and move chart. Use .ChartArea for this.
        With Cht.ChartArea
            .Height = TSht.Range("N2:N14").Height
            .Width = TSht.Range("N2:T2").Width
            .Top = TSht.Range("N2").Top
            .Left = TSht.Range("N2").Left
        End With
    
    End Sub
    

    它负责处理从边框、列删除、分隔线、图表创建和操作等所有方面的工作。这是最好的,这样你的宏就不会到处乱跳。当然,它使用ActiveWorkbook,因此您可以将它放在另一个工作簿中,然后在突出显示包含TraceTable 工作表的工作簿时运行它。

    希望这会有所帮助。

    【讨论】:

    • 它给了我一个错误:Subscript out of rangeSet Shp = ThisWorkbook.Sheets("TraceTable")这一行
    • 这意味着工作表 TraceTable 不存在于您放置此宏的工作簿中。无论如何,只需使用您帖子中的一个:ActiveWorkbook.Worksheets("TraceTable")
    • 它现在再次给我The item with the specified name wasn't found. 错误:Set Shp = .ChartObjects("EIT").Chart
    • 如果不是专有文件,能否把相关文件上传到某处让我看看?
    • 它是专有的,所以很遗憾我无法分享它。不过请给我一分钟,我将进行编辑并输入可以与您分享的相关代码,然后还会提供有关如何设置可用作示例的电子表格的简短说明。
    猜你喜欢
    • 2018-10-05
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多