简体   繁体   English

Excel- vba-到宏生成图表的预设图表轴标签

[英]Excel- vba- to preset chart axis labels for a macro- generated chart

I have a problem. 我有个问题。 Let's say, for simplicity, I'm processing a simple two colunms of data repeatedly and the output is a plot, so I recorded a macro. 假设,为简单起见,我反复处理一个简单的两个数据集,而输出是一个图,因此我记录了一个宏。 But the problem is I want to set the axis labels on the chart, as I want, so as they have (the same) concrete names, every times I run the macro. 但是问题是我每次运行宏时都想根据需要在图表上设置轴标签,以便它们具有(相同)具体名称。 But the vba- macro code does not record any information about the difference between the X and the Y axis, and the result is, that the one axis label is overwritten or, there are two same axis labels in the macro- generated chart. 但是vba宏代码没有记录有关X轴和Y轴之间差异的任何信息,结果是,一个轴标签被覆盖,或者在宏生成的图表中有两个相同的轴标签。 I add: code sample (in the code, I removed all unnecessary data, and such, that prevent the macro from running next time). 我添加了:代码示例(在代码中,我删除了所有不必要的数据,从而阻止了下次运行宏)。 What I want (I apologize for such rude formulation, but I just wanted to strictly state, what is my idea): I would like to have a solution, such, that every time I run the macro, I have a different chart, but with the same axis label names, ie, X-axis: U [V], and Y-axis: I [A] 我想要的(我对这种粗鲁的表述表示歉意,但我只是想严格说明我的想法):我想要一个解决方案,例如,每次运行宏时,我都有一张不同的图表,但是具有相同的轴标签名称,即X轴:U [V],Y轴:I [A]

code: 码:

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth
ActiveChart.ApplyLayout (1)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""I-V char"""
ActiveChart.SeriesCollection(1).XValues = "=hviezd1!$D$2:$D$193"
ActiveChart.SeriesCollection(1).Values = "=hviezd1!$E$2:$E$193"
ActiveChart.Axes(xlValue).AxisTitle.Select
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "I [A]"
ActiveChart.Axes(xlValue).AxisTitle.Select
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "U [V]"

I tried to record the macro by different ways such as throught the tool bar in excel (also more ways throught this), throught the keyboard, but none recorded code contains any information, that could distinguish between X and Y axis. 我试图通过不同的方式来记录宏,例如通过excel中的工具栏(也可以通过这种方式),通过键盘来记录宏,但是没有记录的代码包含任何可以区分X轴和Y轴的信息。

I'm using a 2007 excel (as I read before in other questions, the 2007 excel chart macro recorder is pretty poor and the problem, I'm talking about might be caused by this), but I'm looking for any good answer that can help solve my problem. 我正在使用2007 excel(如我之前在其他问题中所读,2007 excel图表宏记录器非常差,这个问题,我在说的可能是由这个引起的),但是我正在寻找任何好的答案可以帮助解决我的问题。 Thank's for your answer. 感谢您的回答。

Here is a nice VBA script for drawing a bar chart. 这是一个很好的VBA脚本,用于绘制条形图。 you need to make changes in cell values to make it work for you. 您需要更改单元格值以使其适合您。 For me it worked good. 对我来说,效果很好。 This is a kind of note of thanks to all the net users from who I learnt VBA (not expert level yet).... 感谢所有从我那里学习VBA的网络用户(还不是专家级)。...

Sub yourMethodName()

Dim myChart As Chart, cht As ChartObject
Dim rngChart As Range, desinationSheet As String
destinationSheet = ActiveSheet.Name
Set myChart = Charts.Add
Set myChart = myChart.Location(Where:=xlLocationAsObject, Name:=destinationSheet)
myChart.SetSourceData Source:=Range("D37:E38").CurrentRegion, PlotBy:=xlColumns
myChart.ChartType = xlColumnClustered
ActiveSheet.ChartObjects(1).Activate
Set cht = ActiveChart.Parent
'Y-axis value is set up below
cht.Chart.Axes(xlValue).MinimumScale = 1
cht.Chart.Axes(xlValue).MaximumScale = 1
' X axis and Y axiz titles
With cht
'X axis title
cht.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
cht.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "All CRs"
 'y-axis title
cht.Chart.Axes(xlValue, xlPrimary).HasTitle = True
cht.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "% Complete"
End With

'Chart area is set up below
Set rngChart = Range("B12:G33")
cht.Left = rngChart.Left
cht.Top = rngChart.Top
cht.Width = rngChart.Width
cht.Height = rngChart.Height
Range("A37").Select

End Sub

You can use this: 您可以使用此:

With ActiveChart 

 'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True 
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X-Axis" 
 'y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True 
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y-Axis" 
End With 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM