这是给我带来麻烦的代码部分。我合并了消息框,让我知道事情正在达到那个给定点。因此,当我键入“MsgBox xaxis.Address()”时,它的输出类似于 $C$20:$C$42。这就是我希望在 x 轴上绘制的图形。问题是,我的图表实际上并没有显示在 x 轴上……它像整个工作表一样绘制图表。我猜我的问题在于我正在使用的 xaxis 的语法。
Dim StartTime As Range
Dim EndTime As Range
Set StartTime = wb1.Sheets("Sheet2").Range("C:C").Find(wb1.Sheets("Sheet1").Range("$B$2").Text, MatchCase:=False, lookat:=xlWhole)
Set EndTime = wb1.Sheets("Sheet2").Range("C:C").Find(wb1.Sheets("Sheet1").Range("B3").Text, MatchCase:=False, lookat:=xlWhole)
'If Not StartTime Is Nothing Then
'End If
MsgBox StartTime.Address()
MsgBox EndTime.Address()
MsgBox StartTime.Value
MsgBox EndTime.Value
MsgBox "Hi"
Dim xaxis As Range
Dim yaxis As Range
Set xaxis = Range(StartTime.Address & ":" & EndTime.Address)
MsgBox xaxis.Address
'Set xaxis = Range("$C$16", Range("$C$16").End(xlDown))
Set yaxis = xaxis.Offset(0, ColumnOffset:=1)
MsgBox yaxis.Address
'''''''''''''''''''''''
'Dim x As String
'Dim y As String
' x = xaxis.Address
'MsgBox "Hi"
'y = yaxis.Address
'MsgBox x
Set Chrt = Charts.Add
With Chrt
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
'.SeriesCollection(1).Name = "=""Scatter Chart"""
.SeriesCollection(1).Values = yaxis
.SeriesCollection(1).XValues = xaxis
'Titles
.HasTitle = True
.ChartTitle.Characters.Text = "Platen1"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Seconds)"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temp (Deg. C)"
.Axes(xlCategory).HasMajorGridlines = True
'Formatting
.Axes(xlCategory).HasMinorGridlines = False
.Axes(xlValue).HasMajorGridlines = True
.Axes(xlValue).HasMinorGridlines = False
.HasLegend = False
End With
我的总体目标:我有一个电子表格,其中包含每天更新的数据。我需要用户能够输入开始时间、结束时间和机器编号。这需要用于从该数据生成图表。开始和结束时间始终在 C 列中,但特定行总是在变化。所以我要做的是从数据中找到匹配的开始时间和结束时间,然后创建这个范围(开始时间或结束时间)。机器将确定用于 y 轴的数据列,该列仅从 x 轴偏移。
任何帮助将不胜感激。
编辑:我通过在图表创建下方添加以下代码来修复上述问题:
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
我意识到的问题是“xaxis”和“yaxis”正在从 Sheet1 中获取数据,而我希望它从 Sheet2 中获取数据。我该如何声明它应该从 Sheet2 中绘图?
最佳答案
像这样做
Set xaxis = Sheets("Sheet2").Range(StartTime.Address & ":" & EndTime.Address)
Set yaxis = xaxis.Offset(0, ColumnOffset:=1)
关于vba - 散点图绘制excel VBA - 不会绘制我的数据范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28500123/