vba 在 Excel 中将 x 轴的最大值和最小值设置为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22469890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:59:02  来源:igfitidea点击:

Setting maximum and minimum values for x-axis as dates in Excel

excelvba

提问by user2207622

I have a graph that has dates on the x-axis and I'm trying to set maximum and minimum values for this axis using an Excel VBA. Below is my code which doesnt seem to work.Can anyone please help.

我有一个在 x 轴上有日期的图表,我正在尝试使用 Excel VBA 为该轴设置最大值和最小值。下面是我的代码,它似乎不起作用。任何人都可以帮忙。

With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue)
    .MinimumScale = ActiveSheet.Range("C33").Value
    .MaximumScale = ActiveSheet.Range("D54").Value
End With

回答by Werner

xlValuerefers to the y-axis (or value axis). You're interested in adjust the x-axis values (or category axis) which require xlCategory. So use

xlValue指的是 y 轴(或值轴)。您有兴趣调整需要xlCategory. 所以用

With ActiveSheet.ChartObjects(1).Chart.Axes(xlCategory)
    .MinimumScale = ActiveSheet.Range("C33").Value
    .MaximumScale = ActiveSheet.Range("D54").Value
End With

回答by Gerry

I created a chart for a bivariate normal distribution. X1 follows a normal distribution with mu1 and stdev1 and likewise for X2. X1 is along the X axis. I wanted the limits to be within 4 standard deviations of the mean. mywidth and myheight were assigned beforehand. The data start on row 2 since there are titles on row 1. The data for X1 are in the 1st column. n is the number of rows of data.

我为二元正态分布创建了一个图表。X1 服从 mu1 和 stdev1 的正态分布,X2 也是如此。X1 沿 X 轴。我希望限制在平均值的 4 个标准偏差内。mywidth 和 myheight 是预先分配的。数据从第 2 行开始,因为第 1 行有标题。 X1 的数据位于第 1 列。n 是数据的行数。

mysheetname = ActiveSheet.Name
Set mychart = Sheets(mysheetname).ChartObjects.Add(Left:=mywidth, Top:=myheight + 2, Width:=400, Height:=250)
mychart.Chart.ChartType = xlXYScatter
mychart.Chart.SeriesCollection.NewSeries
mychart.Chart.SeriesCollection(1).Values = Range(Cells(outputrow + 1, outputcol + 1), Cells(outputrow + n, outputcol + 1))
mychart.Chart.SeriesCollection(1).XValues = Range(Cells(outputrow + 1, outputcol), Cells(outputrow + n, outputcol))
mychart.Chart.HasLegend = False
mychart.Chart.Axes(xlValue).MinimumScale = mu2 - 4 * sigma2
mychart.Chart.Axes(xlValue).MaximumScale = mu2 + 4 * sigma2
mychart.Chart.Axes(xlCategory).MinimumScale = mu1 - 4 * sigma1
mychart.Chart.Axes(xlCategory).MaximumScale = mu1 + 4 * sigma1