2017-06-21 192 views
0

我正在嘗試更改用vba生成的圖表的位置。目前,它只是從列中獲取數據,可能會或可能不會更改大小。我知道我沒有'Chart1'識別我的代碼,但我無法弄清楚在哪裏聲明它不會爲圖表創建另一個表單。使用VBA更改圖表的位置

reportsheet.Select 
ActiveSheet.Range("a4", ActiveSheet.Range("a4").End(xlDown)).Select 
ActiveSheet.Shapes.AddChart.Select 

With ActiveSheet.Shapes("Chart1") 
    .Left = Range("A40").Left 
    .Top = Range("A40").Top 
End With 

回答

0

您可以更改活動圖表的名稱,然後爲其分配屬性。

嘗試......

reportsheet.Select 
    ActiveSheet.Range("A4", ActiveSheet.Range("A4").End(xlDown)).Select 
    ActiveSheet.Shapes.AddChart.Select 

    ActiveChart.Parent.Name = "Chart1" 

    With ActiveSheet.Shapes("Chart1") 
     .Left = Range("A40").Left 
     .Top = Range("A40").Top 
    End With 
+0

將該行添加到VBA中,它給我一個運行時錯誤,指出找不到指定名稱的項目。它突出顯示With ActiveSheet.Shapes(「Chart1」)作爲錯誤行。 –

+0

請嘗試使用獨特的名稱 – Maddy

0

我用做圖這個樣子。請參閱波紋管。

Sub test() 
    Dim Ws As Worksheet 
    Set Ws = ActiveSheet 

    InsertCharts 20, Ws 
    InsertCharts 30, Sheets("Sheet1") 
End Sub 

Sub InsertCharts(n As Integer, Ws As Worksheet) 

    Dim Cht As Shape 
    Dim t As Single, w As Integer, h As Integer, x As Integer 
    Dim i As Integer 

    With Ws 
     If .ChartObjects.Count > 0 Then 
      .ChartObjects.Delete 
     End If 

     x = 0 
     t = .Range("a26").Top 
     w = 217.1338582677 
     h = 203.5275590551 
     For i = 1 To n 

       Set Cht = .Shapes.AddChart(, x, t, w, h) 
      If i Mod 5 = 0 Then 
       t = .Range("a26").Top 
       x = x + w + 20 
      Else 
       t = t + h + 20 
      End If 

     Next i 
    End With 
End Sub