ActiveChart.SetSourceData 來源資料問題 (己解決)
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 2# FAlonso
[會出現型態不符合的警號,該如何避免?]
你的錯誤在於原本的程式在執行時
ActiveSheet.Selection 是 .ChartObjects(1) 而不是 RANGE
修改你的程式如下
b2 b3 請直接輸入日期 即可
- Sub plotgraph()
- Dim myrange As Range, error As Boolean, x As Integer, y As Integer
- Dim Rng As Range
- error = False
- x = 0
- y = 0
- With Sheet1
- .Activate
- .[a1].Activate
- '[會出現型態不符合的警號,該如何避免?]
- '你的錯誤在於原本的程式在執行時
- 'ActiveSheet.Selection 是 .ChartObjects(1) 而不是 RANGE
- Set Rng = .Range(.[a8], .[a65536].End(xlUp))
- If .[b2] = "" Or .[b3] = "" Then
- MsgBox "No date"
- error = True
- ElseIf .[b2] > .[b3] Then
- MsgBox .[b2] & ">" & .[b3]
- error = True
- ElseIf Rng(1) > .[b2] Then
- MsgBox "Before starting period"
- error = True
- ElseIf .[b3] > Rng(Rng.Count) Then
- MsgBox "After ending period"
- error = True
- End If
- If error = True Then Exit Sub
- For Each myrange In Rng
- If myrange >= .[b2] Then
- x = myrange.Row - 1
- Exit For
- End If
- Next
- For Each myrange In Rng
- If .[b3] <= myrange Then
- y = myrange.Row - 1
- Exit For
- End If
- Next
- If Sheet1.ChartObjects.Count = 0 Then Charts.Add
- .ChartObjects(1).Select
- ActiveChart.ChartType = xlLineMarkers
- ActiveChart.Location xlLocationAsObject, Name:="Sheet1"
- ActiveChart.HasDataTable = False
- .Activate
- If [b4] = "開盤" Then
- ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",B" & x & ":B" & y), PlotBy:=xlColumns
- ElseIf .[b4] = "最高" Then
- ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",C" & x & ":C" & y), PlotBy:=xlColumns
- ElseIf .[b4].Value = "最低" Then
- ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",D" & x & ":D" & y), PlotBy:=xlColumns
- ElseIf .[b4].Value = "收盤" Then
- ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",E" & x & ":E" & y), PlotBy:=xlColumns
- End If
- End With
- End Sub
複製代碼 |
|
|
|
|
|
|
|