[英]Change range in chart from userform input
我已經制作了一個用戶表格,以便一個人可以輸入開始日期和結束日期,以便折線圖可以顯示所需的信息。 目前,除了范圍更新語法,我已經可以進行所有操作。
我將開始日期數據的地址另存為Ad,將結束日期地址的地址另存為Add(均為字符串)。
然后,我嘗試使用這些設置范圍,但是我做錯了。 這是代碼。
Dim CellX1 As Integer
Dim CellY1 As Integer
Dim CellX2 As Integer
Dim CellY2 As Integer
Dim Ad As String
Dim Add As String
Sheets("Data").Activate
Cells(CellY1, CellX1).Activate
Ad = ActiveCell.Address 'set start address
Cells(CellY2, CellX2).Activate
Add = ActiveCell.Address 'set end address
Sheets("Graph").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""A3"""
這是我無法使用的代碼行:
ActiveChart.SeriesCollection(1).Values = "=Data!$Ad:Add"
ActiveChart.SeriesCollection(1).XValues = "=Time!$E:$F"
應該能夠Set
它們Set
為Range
版本。 您也將更好地將系列變量分配給實際Ranges
而不是將地址分配為字符串。 確實,您應該直接Set
下面的內容。
完整的代碼應類似於:
Dim CellX1 As Integer
Dim CellY1 As Integer
Dim CellX2 As Integer
Dim CellY2 As Integer
Dim Ad As Range
Dim Add As Range
Set Ad = Sheets("Data").Cells(CellY1, CellX1) 'set start address
Set Add = Sheets("Data").Cells(CellY2, CellX2) 'set end address
Sheets("Graph").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = Range("A3")
ActiveChart.SeriesCollection(1).Values = Range(Ad, Add)
ActiveChart.SeriesCollection(1).XValues = Worksheets("Time").Range("$E:$F")
請注意,我更改了Ad
和Add
to Range
變量的類型。 這樣可以更輕松地為圖表創建開始/結束Range
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.