Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加水平轴标签-VBA Excel_Vba_Excel_Charts_Axis Labels - Fatal编程技术网

添加水平轴标签-VBA Excel

添加水平轴标签-VBA Excel,vba,excel,charts,axis-labels,Vba,Excel,Charts,Axis Labels,我有一个宏可以创建图形。我希望VBA从电子表格中读取一个范围,并使用水平轴标签的值。基本上,我想制作这个图表: 如下所示(在底部添加月份) 谢谢 宏: Sub AddChartSheet() 'Variable declaration Dim chtChart As Chart Dim name1 As String 'Name is currently used for the title of the new tab where the chart is

我有一个宏可以创建图形。我希望VBA从电子表格中读取一个范围,并使用水平轴标签的值。基本上,我想制作这个图表:

如下所示(在底部添加月份)

谢谢

宏:

Sub AddChartSheet()

   'Variable declaration
   Dim chtChart As Chart
   Dim name1 As String

   'Name is currently used for the title of the new tab where the chart is     created and the chart title
   name1 = "AHU-10-8"

   'Create a new chart.
   Set chtChart = Charts.Add
   With chtChart
  '.Name is the name of the tab where the new Chart is created
  .Name = name1

  .ChartType = xlLine
  'Link to the source data range.
  .SetSourceData Source:=Sheets(3).Range("A1:B5861"), _
     PlotBy:=xlColumns
  .HasTitle = True
  .ChartTitle.Text = name1
  .Axes(xlCategory, xlPrimary).HasTitle = True
  .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
  .Axes(xlValue, xlPrimary).HasTitle = True
  .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Valve Position (-)"

  myFileName = name1 & ".png"
  chtChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"

   End With
End Sub

你能分享你的宏或工作簿吗

我不确定您是如何设置数据的,但您可以将为水平标签选择的数据格式更改为日期格式。或者,在VBA中,您可以将选择更改为“mmmm”的数字格式,以仅显示月份

Selection.NumberFormat = "mmmm"

以下几点应该行得通。请记住在以下代码中调整图表名称

ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.NumberFormat = "mmmm"

调整用于日期的数据系列(水平轴)。您可以添加以下内容

ActiveSheet.ChartObjects("Chart 15").Activate
ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$D$5:$D$19"
注意:您首先需要选择图表并将我拥有的范围调整为您需要的范围

或者你可以加上

.SeriesCollection(1).XValues = "=Sheet1!$D$5:$D$19"
在代码之间

.SetSourceData Source:=Sheets(3).Range("A1:B5861"), _
 PlotBy:=xlColumns


我在操作中添加了宏。谢谢!我的问题是,我不知道如何为水平标签选择数据。显示的数字是默认值。好的,现在我在x轴上得到了月份,但每次读数得到的月份不同。我有一个不同的列,日期格式如下:2016年2月1日。有没有办法用它来计算实际月份?谢谢您现在使用的是什么类型的数据?您可以通过更改范围来调整使用其他日期列。但这是我的第一个问题。如何通过vba将范围指定给x轴标签?
.HasTitle = True