VBA定位excel图形
我在创建此图表时录制了一个宏,生成:VBA定位excel图形,excel,vba,Excel,Vba,我在创建此图表时录制了一个宏,生成: Sub Macro13() Columns("A:B").Select ActiveSheet.Shapes.AddChart.Select ActiveChart.SetSourceData Source:=Range("Sheet!$A$1:$B$100) ActiveChart.ChartType = xlColumnClustered ActiveChart.Legend.Select Selecti
Sub Macro13()
Columns("A:B").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet!$A$1:$B$100)
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Legend.Select
Selection.Delete
ActiveChart.ChartTitle.Text = "Title"
'ActiveChart.Left = ActiveSheet.Cells(4, 4) Doesnt work??
End Sub
我想把图表放在特定的单元格上。但是,Left()方法在ActiveChart上不起作用,我找到的所有解决方案都是基于以下几点:
ActiveSheet.Graphs(0).Left() =
我讨厌它,因为我不想猜表上有多少图表
Sub newChart()
Dim newChartPositionLeft As Integer
Dim newChartPositionTop As Integer
Dim lastChartAdded As ChartObject
Dim firstChart As Boolean
'Turn on error handling. If trying to access a previous chart fails, you know this is the first chart to be added
On Error Resume Next
Set lastChartAdded = ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
If Err.Number <> 0 Then
firstChart = True
Else
firstChart = False
newChartPositionTop = lastChartAdded.Top + lastChartAdded.Height + 5 'Increase this number to add padding between charts
newChartPositionLeft = lastChartAdded.Left 'Aligning the new chart with the previous one
End If
On Error GoTo 0
'Your original code
Columns("A:B").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet!$A$1:$B$100")
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Legend.Select
Selection.Delete
'Positioning the new chart
If firstChart Then
ActiveChart.Parent.Left = Range("B2").Left
ActiveChart.Parent.Top = Range("B2").Top
Else
ActiveChart.Parent.Left = newChartPositionLeft
ActiveChart.Parent.Top = newChartPositionTop
End If
End Sub
在我创建上述图表时,是否有方法使用局部变量定位图表?这将移动图表对象,使左上角位于B2范围内:
ActiveChart.Parent.Left = Range("B2").Left
ActiveChart.Parent.Top = Range("B2").Top
ActiveChart是图表-父对象是图表对象(图表的容器)。这将移动图表对象,使左上角在B2范围内:
ActiveChart.Parent.Left = Range("B2").Left
ActiveChart.Parent.Top = Range("B2").Top
ActiveChart是图表-父对象是图表对象(图表的容器)。这是对上一个问题的回答: 在我创建上面的图表时,有没有一种方法可以使用局部变量来定位图表 这将帮助您根据工作表上的其他图表定位新图表
Sub newChart()
Dim newChartPositionLeft As Integer
Dim newChartPositionTop As Integer
Dim lastChartAdded As ChartObject
Dim firstChart As Boolean
'Turn on error handling. If trying to access a previous chart fails, you know this is the first chart to be added
On Error Resume Next
Set lastChartAdded = ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
If Err.Number <> 0 Then
firstChart = True
Else
firstChart = False
newChartPositionTop = lastChartAdded.Top + lastChartAdded.Height + 5 'Increase this number to add padding between charts
newChartPositionLeft = lastChartAdded.Left 'Aligning the new chart with the previous one
End If
On Error GoTo 0
'Your original code
Columns("A:B").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet!$A$1:$B$100")
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Legend.Select
Selection.Delete
'Positioning the new chart
If firstChart Then
ActiveChart.Parent.Left = Range("B2").Left
ActiveChart.Parent.Top = Range("B2").Top
Else
ActiveChart.Parent.Left = newChartPositionLeft
ActiveChart.Parent.Top = newChartPositionTop
End If
End Sub
Sub-newChart()
将newChartPositionLeft设置为整数
Dim newChartPositionTop为整数
Dim LastChart已添加为ChartObject
将firstChart设置为布尔值
'启用错误处理。如果尝试访问以前的图表失败,您知道这是要添加的第一个图表
出错时继续下一步
设置lastChartAdded=ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
如果错误号为0,则
firstChart=True
其他的
firstChart=False
newChartPositionTop=lastChartAdded.Top+lastChartAdded.Height+5'增加此数字可在图表之间添加填充
newChartPositionLeft=lastChartAdded.Left'将新图表与上一个图表对齐
如果结束
错误转到0
'您的原始代码
列(“A:B”)。选择
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData源:=范围(“工作表!$A$1:$B$100”)
ActiveChart.ChartType=xlColumnClustered
ActiveChart.Legend.Select
选择。删除
定位新图表
如果是第一张图表的话
ActiveChart.Parent.Left=范围(“B2”).Left
ActiveChart.Parent.Top=范围(“B2”).Top
其他的
ActiveChart.Parent.Left=newChartPositionLeft
ActiveChart.Parent.Top=newChartPositionTop
如果结束
端接头
这是对您最后一个问题的回答:
在我创建上面的图表时,有没有一种方法可以使用局部变量来定位图表
这将帮助您根据工作表上的其他图表定位新图表
Sub newChart()
Dim newChartPositionLeft As Integer
Dim newChartPositionTop As Integer
Dim lastChartAdded As ChartObject
Dim firstChart As Boolean
'Turn on error handling. If trying to access a previous chart fails, you know this is the first chart to be added
On Error Resume Next
Set lastChartAdded = ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
If Err.Number <> 0 Then
firstChart = True
Else
firstChart = False
newChartPositionTop = lastChartAdded.Top + lastChartAdded.Height + 5 'Increase this number to add padding between charts
newChartPositionLeft = lastChartAdded.Left 'Aligning the new chart with the previous one
End If
On Error GoTo 0
'Your original code
Columns("A:B").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet!$A$1:$B$100")
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Legend.Select
Selection.Delete
'Positioning the new chart
If firstChart Then
ActiveChart.Parent.Left = Range("B2").Left
ActiveChart.Parent.Top = Range("B2").Top
Else
ActiveChart.Parent.Left = newChartPositionLeft
ActiveChart.Parent.Top = newChartPositionTop
End If
End Sub
Sub-newChart()
将newChartPositionLeft设置为整数
Dim newChartPositionTop为整数
Dim LastChart已添加为ChartObject
将firstChart设置为布尔值
'启用错误处理。如果尝试访问以前的图表失败,您知道这是要添加的第一个图表
出错时继续下一步
设置lastChartAdded=ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
如果错误号为0,则
firstChart=True
其他的
firstChart=False
newChartPositionTop=lastChartAdded.Top+lastChartAdded.Height+5'增加此数字可在图表之间添加填充
newChartPositionLeft=lastChartAdded.Left'将新图表与上一个图表对齐
如果结束
错误转到0
'您的原始代码
列(“A:B”)。选择
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData源:=范围(“工作表!$A$1:$B$100”)
ActiveChart.ChartType=xlColumnClustered
ActiveChart.Legend.Select
选择。删除
定位新图表
如果是第一张图表的话
ActiveChart.Parent.Left=范围(“B2”).Left
ActiveChart.Parent.Top=范围(“B2”).Top
其他的
ActiveChart.Parent.Left=newChartPositionLeft
ActiveChart.Parent.Top=newChartPositionTop
如果结束
端接头