Selection with one object

A

Arne Hegefors

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!
 
A

Andy Pope

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
 
A

Arne Hegefors

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:
 
A

Andy Pope

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top