0

I am working on Stock OHLC Chart present in Sheets("Exhibit") and selecting Data from Sheets("75Min"). However i am succeeding to choose appropriate data range but can not add at Line Nu 15 .SetSourcedata, could you please help me to get out from this problem Code is as follows.

Sub Edit75MinChartToOHLCCandlestickChart()

Dim OHLCChart As ChartObject

Dim LastRow As Integer

Dim RngSt As Integer

    Sheets("75Min").Select

    Range("A1").Select

    Range("A1").End(xlDown).Select

    LastRow = ActiveCell.Row

    RngSt = LastRow - 59

    RngEnd = LastRow + 15

    Set OHLCChart = ThisWorkbook.Worksheets("Exhibit").ChartObjects(1)

        With OHLCChart.Chart 'Worksheets("Exhibit").ChartObjects("Chart 2").Chart

            .SetSourceData ThisWorkbook.Worksheets("75Min").Range(RngSt, RngEnd)

            .ChartType = xlStockOHLC

            .HasTitle = True

            .ChartTitle.Text = "75Min Candlestick chart"

            .Axes(xlValue, xlPrimary).HasTitle = False

            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Price"

            .PlotArea.Format.Fill.ForeColor.RGB = RGB(242, 242, 242)

            .ChartArea.Format.Line.Visible = msoFalse

            .Parent.Name = "OHLC Chart"

        End With

    End Sub

Thank You

4

2 回答 2

0

您可以包括这些行来调整 Y 轴刻度:

Dim Ymin As Double, Ymax As Double
Ymin = WorksheetFunction.Min(MyRng)
Ymax = WorksheetFunction.Max(MyRng)

With OHLCChart.Chart.Axes(xlValue)
  .MinimumScale = Ymin
  .MaximumScale = Ymax
End With

但这些可能是丑陋的轴限制。我在 Excel VBA 中编写了一个名为“Calculate Nice Axis Scales”的教程,其中包含一个算法,可以稍微向外调整最小值和最大值,以便刻度更易于阅读。

于 2021-05-06T22:54:37.487 回答
0

最好避免SelectActivate。您的范围不是有效范围。您是说Range(#,#),这是无效的,我怀疑您是否试图获得整行。假设您只想要第一列,这将起作用。如果需要标签,可以从 1 开始扩展列。

Sub Edit75MinChartToOHLCCandlestickChart()

    Dim OHLCChart As ChartObject

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    
    Set ws1 = ThisWorkbook.Worksheets("75Min")
    Set ws2 = ThisWorkbook.Worksheets("Exhibit")
    Dim MyRng As Range
    Dim LastRow As Long
    Dim RngSt As Long
    Dim RngEnd as Long
    LastRow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row

    RngSt = LastRow - 59

    RngEnd = LastRow + 15
    
    Set MyRng = ws1.Range(ws1.Cells(RngSt, 1), ws1.Cells(RngEnd, 1))
    
    Set OHLCChart = ws2.ChartObjects(1)

        With OHLCChart.Chart 'Worksheets("Exhibit").ChartObjects("Chart 2").Chart

            .SetSourceData MyRng
于 2021-04-30T11:42:23.253 回答