我是vba的新手,並試圖在excel頁面上放置vba圖表。以下是代碼。如果我更換:定義vba圖表位置
Set c = c.Location(Where:=xlLocationAsObject, Name:=chLoc.Parent.Name)
與
Set c = c.Location(Where:=xlLocationAsObject, Name:="Sheet1")
然後代碼工作。否則我得到 「錯誤1004法‘’對象」範圍_Worksheet失敗」 和行發生錯誤:
Set chLoc = sh2.Range(sh2.[a1:g10])
這裏是代碼 - 感謝,如果您有好的資源,VBA圖表我將不勝感激任意方向:
Sub Chart()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim xaxis As Range
Dim yaxis As Range
Set sh1 = ActiveWorkbook.Sheets("Spon Email Performance Graph")
Set sh2 = ActiveWorkbook.Sheets("Graphs")
Set xaxis = sh1.Range(sh1.[b15], sh1.[b15].End(xlDown))
Set yaxis = sh1.Range(sh1.[g15], sh1.[g15].End(xlDown))
Dim chLoc As Range
Set chLoc = sh2.Range(sh2.[a1:g10])
' Worksheets("Graphs").ChartObjects.Delete
Dim c As Chart
Set c = Charts.Add
Set c = c.Location(Where:=xlLocationAsObject, Name:=chLoc.Parent.Name)
With c
.ChartType = xlColumnClustered
' set other chart properties
End With
Dim s As Series
Set s = c.SeriesCollection.NewSeries
With s
.Values = yaxis
.XValues = xaxis
' set other series properties
End With
End Sub
IIF你想要的代碼更可靠,而不是sh2.Range(SH2 [A1:G10]),使用sh2.Range( 「A1:G10」)。 –