我在Excel中使用正弦计算器
我尝试将图表插入表格
图表应为正弦波,Y轴为幅度,X轴为时间
问题是我得到一张带有两个图形的图表:正弦曲线,根据Y柱,线条 - 根据X柱。
这是我的代码:
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
我得到类似的东西: Chart with sine wave
时钟频率,分频器和DAC分辨率是常数 用户更改所需的频率,幅度和相位 工作表自动计算点数,计算点数(时间,dac_value),并根据点数创建所需的图表。
如上所述,结果我得到带有两个图表的图表(X轴是点数,Y轴 - 是幅度(DAC值))。
我需要图表只有一个图表(正弦),X轴作为时间(列A),Y轴作为幅度(列B)。
答案 0 :(得分:1)
在创建图表之前清除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