ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Selection with one object (https://www.excelbanter.com/excel-programming/371769-selection-one-object.html)

Arne Hegefors

Selection with one object
 
I have a macro that lets the user first select a number of charts and then
when the user presses a button the charts sizes and color are eing changed.
The problem is that it does not works if I only have one chart selected. When
I have two or more it works fine. My code (relevant parts) is:

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
End Sub

In this sub I check the selection to see if the selected objects really are
charts. I assume that there is something wrong here on the first line of the
For-loop, but I do not know how it should be or if the problem might be
elsewhere. Any help appreciated! Thanks alot!

Andy Pope

Selection with one object
 
Expand the code a little more to check TypeName of selection.

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

If TypeName(Selection) = "DrawingObjects" Then
' group of possible charts
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
ElseIf TypeName(Selection) = "ChartObject" Then
' single chartobject
Set currentChart = Selection
Call linjeDiagramKnapp(currentChart)
ElseIf TypeName(Selection) = "ChartArea" Then
' activechart
Set currentChart = Selection.Parent
Call linjeDiagramKnapp(currentChart)
End If

End Sub

Cheers
Andy

Arne Hegefors wrote:
I have a macro that lets the user first select a number of charts and then
when the user presses a button the charts sizes and color are eing changed.
The problem is that it does not works if I only have one chart selected. When
I have two or more it works fine. My code (relevant parts) is:

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
End Sub

In this sub I check the selection to see if the selected objects really are
charts. I assume that there is something wrong here on the first line of the
For-loop, but I do not know how it should be or if the problem might be
elsewhere. Any help appreciated! Thanks alot!


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

Arne Hegefors

Selection with one object
 
Hi Andy! Your code works fine but it creates another problem. When I pass the
chart(s) along to another sub as an argument that sub does not quite know
what type of object it is. It seems like that at least because when I try to
make changes to the charts in the sub I get an error either way I write it
depending on if I have selected one or two charts. In the sub:

Sub linjeDiagramKnapp(argChart As Object)

if I write: With argChart.Chart
i get error if I have selected only one chart but it works fine if I have
selected two charts. If i write:
with argChart
and i only have one chart selected it works fine but with two charts i get
error. do you have any idea of how to solve that probelm? any help
appreciated!

"Andy Pope" skrev:

Expand the code a little more to check TypeName of selection.

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

If TypeName(Selection) = "DrawingObjects" Then
' group of possible charts
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
ElseIf TypeName(Selection) = "ChartObject" Then
' single chartobject
Set currentChart = Selection
Call linjeDiagramKnapp(currentChart)
ElseIf TypeName(Selection) = "ChartArea" Then
' activechart
Set currentChart = Selection.Parent
Call linjeDiagramKnapp(currentChart)
End If

End Sub

Cheers
Andy

Arne Hegefors wrote:
I have a macro that lets the user first select a number of charts and then
when the user presses a button the charts sizes and color are eing changed.
The problem is that it does not works if I only have one chart selected. When
I have two or more it works fine. My code (relevant parts) is:

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
End Sub

In this sub I check the selection to see if the selected objects really are
charts. I assume that there is something wrong here on the first line of the
For-loop, but I do not know how it should be or if the problem might be
elsewhere. Any help appreciated! Thanks alot!


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info


Andy Pope

Selection with one object
 
Now I can see the header for linjeDiagramKnapp routine I can see that
the activechart part is not quite right. Needs another Parent reference
in order to pass the same object level as the others.

Try this modification.


' activechart
Set currentChart = Selection.Parent.Parent
Call linjeDiagramKnapp(currentChart)


This is what I used to test the various selections.

Sub linjeDiagramKnapp(argChart As Object)

With argChart.Chart
MsgBox "Has " & .SeriesCollection.Count & " series"
End With

End Sub

Cheers
Andy

Arne Hegefors wrote:
Hi Andy! Your code works fine but it creates another problem. When I pass the
chart(s) along to another sub as an argument that sub does not quite know
what type of object it is. It seems like that at least because when I try to
make changes to the charts in the sub I get an error either way I write it
depending on if I have selected one or two charts. In the sub:

Sub linjeDiagramKnapp(argChart As Object)

if I write: With argChart.Chart
i get error if I have selected only one chart but it works fine if I have
selected two charts. If i write:
with argChart
and i only have one chart selected it works fine but with two charts i get
error. do you have any idea of how to solve that probelm? any help
appreciated!

"Andy Pope" skrev:


Expand the code a little more to check TypeName of selection.

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

If TypeName(Selection) = "DrawingObjects" Then
' group of possible charts
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
ElseIf TypeName(Selection) = "ChartObject" Then
' single chartobject
Set currentChart = Selection
Call linjeDiagramKnapp(currentChart)
ElseIf TypeName(Selection) = "ChartArea" Then
' activechart
Set currentChart = Selection.Parent
Call linjeDiagramKnapp(currentChart)
End If

End Sub

Cheers
Andy

Arne Hegefors wrote:

I have a macro that lets the user first select a number of charts and then
when the user presses a button the charts sizes and color are eing changed.
The problem is that it does not works if I only have one chart selected. When
I have two or more it works fine. My code (relevant parts) is:

Public Sub arrayLoop()
Dim obj As Object
Dim currentChart As Object

For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart)
End If
Next
End Sub

In this sub I check the selection to see if the selected objects really are
charts. I assume that there is something wrong here on the first line of the
For-loop, but I do not know how it should be or if the problem might be
elsewhere. Any help appreciated! Thanks alot!


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info


All times are GMT +1. The time now is 05:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com