插入行时VBA图表位置会更改

时间:2014-07-17 18:19:29

标签: vba excel-vba excel

我编写了以下代码来添加图表并将其放在带有数据的工作表上:

Dim sh As Worksheet
Dim chrteit As Chart
lastrows = Range("A2").End(xlDown).Row

Set sh = ActiveWorkbook.Worksheets("TraceTable")
Set chrteit = sh.Shapes.AddChart.Chart
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))

    .HasTitle = True
    .ChartTitle.Text = "EIT"
    .Parent.Height = Range("N2:N14").Height
    .Parent.Width = Range("N2:T2").Width
    .Parent.top = Range("N2").top
    .Parent.Left = Range("N2").Left

End With

问题是,在我的模块中稍后我有一个宏,如果两个数据点不同,它将在两个数据点之间产生整行,如下所示:

Private Sub Dividers()

Dim DividerRange As Range, lastrow As Long, k As Integer, counter As Integer

lastrow = Range("C2").End(xlDown).Row

Set DividerRange = Range(Cells(2, 3), Cells(lastrow, 3))
counter = 0

For k = 2 To DividerRange.Count
    If DividerRange(k + counter).Value <> DividerRange(k + counter - 1).Value Then
    DividerRange(k + counter).EntireRow.Insert
    counter = counter + 1
    Else
End If
Next k

End Sub

通过添加整行,它会更改图表的高度及其位置。我希望它是一个固定的位置,我该怎么做?我不会改变第二个代码,而是第一个代码,但让我知道你们有任何解决方案,谢谢!

1 个答案:

答案 0 :(得分:0)

将此行添加到第一个程序:

chrteit.Placement = xlFreeFloating

这与右键单击,格式化图表区域,属性相同:不要移动或调整单元格大小。 | 或者您可以将该方法放在With块中,因此:

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))

    .HasTitle = True
    .ChartTitle.Text = "EIT"
    .Parent.Height = Range("N2:N14").Height
    .Parent.Width = Range("N2:T2").Width
    .Parent.top = Range("N2").top
    .Parent.Left = Range("N2").Left

    .Placement = xlFreeFloating
End With