[英]Creating Excel chart with VBA - wrong result
I did sine calculator in Excel. 我在Excel中做了正弦计算器。
I try to insert chart into sheet. 我尝试将图表插入工作表。
The chart should be sine wave with Y-axis as Amplitude, and X-axis as Time. 图表应为正弦波,Y轴为振幅,X轴为时间。
The problem is that I get a chart with two graphs: sine, according to Y-column, and line - according to the X-column. 问题是我得到了带有两个图形的图表:根据Y列为正弦图,根据X列为折线图。
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. 时钟频率,分频器和DAC分辨率为常数。
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. 工作表自动计算点数,计算点数(时间,dac_value),并根据点数创建所需的图表。
As mentioned above, as a result I get chart with two graphs (X-axis is number of point, Y-axis - is amplitude (DAC value)). 如上所述,结果得到带有两个图形的图表(X轴是点数,Y轴-是幅度(DAC值))。
I need chart with one graph only (sine), with X-axis as Time (Column A), and with Y-axis as Amplitude (Column B). 我只需要一个图(正弦),X轴作为时间(A列),Y轴作为幅度(B列)的图表。
Clear A1 before you create the chart and then reinstate the title afterwards: 创建图表之前,请清除A1,然后再恢复标题:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.