简体   繁体   中英

Creating Excel chart with VBA - wrong result

I did sine calculator in Excel.
I try to insert chart into sheet.
The chart should be sine wave with Y-axis as Amplitude, and X-axis as Time.
The problem is that I get a chart with two graphs: sine, according to Y-column, and line - according to the X-column.
Here my code:

Public oneTimeFlag As Integer

Sub calc()

    Range("A3", Range("A2").End(xlDown)).Clear
    Range("B2", Range("B2").End(xlDown)).Clear

    Range("A2").Value = "0"

    lw = Int(Range("$I$3").Value + 1)

    If lw >= 4 And lw < 21000 Then

        Range("A3").Select

        ActiveCell.Formula = "=(2*PI()/$I$3)+A2"

        Range("A3:A" & lw).FillDown

        Range("B2").Select

        ActiveCell.Formula = "=ROUNDDOWN(POWER(2,$G$2)/2 + SIN(($F$7*2*PI()/360) + A2)*((POWER(2,$G$2)/2) -1), 0)"

        Range("B2:B" & lw).FillDown

        AddOrUpdateChartSheet (lw)

    Else
        MsgBox "Nof points must be 4 at least and less than 21000!"
    End If

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$F$5" Or Target.Address = "$F$6" Or Target.Address = "$F$7" Then
        Dim rng As Range
        Set rng = Range(Selection.Address)
        Call calc
        rng.Select

    End If
End Sub

Sub AddOrUpdateChartSheet(ByVal lw As Integer)
    Dim chtChart As Chart
    If oneTimeFlag <> 1 Then
        oneTimeFlag = 1

        Set chtChart = Charts.Add
        Set chtChart = chtChart.Location(Where:=xlLocationAsObject, Name:="Sheet1")
        With chtChart            
            .ChartType = xlLine


            .SetSourceData (ActiveSheet.Range("A1:B" & lw))
            .HasTitle = True
            .ChartTitle.Text = "Sine"

            With .Parent 
              .Name = "Sine"
            End With
        End With


    Else    
        Dim objChrt As ChartObject        
        Dim sineChartExists As Boolean
        sineChartExists = False

        For Each objChrt In ActiveSheet.ChartObjects
            If objChrt.Name = "Sine" Then
                sineChartExists = True
            End If
        Next

        If sineChartExists = False Then
            oneTimeFlag = 0
            AddOrUpdateChartSheet (lw)
        Else
            Set objChrt = ActiveSheet.ChartObjects("Sine")
            Set chtChart = objChrt.Chart

            With chtChart
                .SetSourceData (ActiveSheet.Range("A1:B" & lw))
            End With
        End If
    End If    
End Sub

I get something similar to: Chart with sine wave

Clock frequency, divider and DAC resolution are constants.
User changes the Needed frequency, amplitude and phase.
The sheet automatic calculates the number of points, calculates points (time, dac_value) and according to number of points creates the needed chart.

As mentioned above, as a result I get chart with two graphs (X-axis is number of point, Y-axis - is amplitude (DAC value)).
I need chart with one graph only (sine), with X-axis as Time (Column A), and with Y-axis as Amplitude (Column B).

Clear A1 before you create the chart and then reinstate the title afterwards:

Sub calc()

    Range("A1", Range("A2").End(xlDown)).Clear
    Range("B2", Range("B2").End(xlDown)).Clear

    lw = Int(Range("$I$3").Value + 1)

    If lw >= 4 And lw < 21000 Then
        Range("A2").Value = "0"

        Range("A3:A" & lw).Formula = "=(2*PI()/$I$3)+A2"

        Range("B2:B" & lw).Formula = "=ROUNDDOWN(POWER(2,$G$2)/2 + SIN(($F$7*2*PI()/360) + A2)*((POWER(2,$G$2)/2) -1), 0)"

        AddOrUpdateChartSheet lw
        Range("A1").Value = "X (Time)"

    Else
        MsgBox "Nof points must be 4 at least and less than 21000!"
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM