Argument not optional

A

Arne Hegefors

Hello (again)! I am really having big trouble with a seemingly easy
application. I have some buttons that when pressed changes selected charts so
that they look a certain way. I have two buttons and the code for the two
different buttons, one is working fine but the other gives me the error
message "Argument not optional" (as I had not selected any charts but I
have). The strange thing is that the codes for the two buttons are basically
xeroxed from each other ie none or both should give error. I cannot
understand why this is the case. The code for the two buttons is:

Set linjeDiagramKnapp = .Controls.Add(Type:=msoControlButton)
With linjeDiagramKnapp
.Caption = "Linjediagram"
.Style = msoButtonCaption
''' msoButtonAutomatic, msoButtonIcon, msoButtonCaption,
''' or msoButtonIconandCaption
.BeginGroup = True
.OnAction = "ChartModul1.arrayLoop"
End With
'slut linjediagramknapp

'stapeldiagramknapp
Set stapelDiagramKnapp = .Controls.Add(Type:=msoControlButton)
With stapelDiagramKnapp
.Caption = "Stapeldiagram"
.Style = msoButtonCaption
''' msoButtonAutomatic, msoButtonIcon, msoButtonCaption,
''' or msoButtonIconandCaption
.BeginGroup = True
.OnAction = "ChartModul2.arrayLoops"
End With
'slut stapeldiagramknapp

and their respective subs are (first a loop that checks to see if selected
objects truly are charts, thanks very much to NickHK and everybody else who
patiently helped me with this).

Sub arrayLoops()
Dim obj As Object
Dim currentChart As Object

'loopar igenom de selekterade objekten. om ett objekt är en Chart så
'skickas det vidare till Sub stapelDiagramKnapp.
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call stapelDiagramKnapp(currentChart) 'skickar Chart
End If
Next
'slut på loop
End Sub

Sub stapelDiagramKnapp(argChart As Object)

Dim mySize As Double
With argChart.Chart
'här börjar inställningen för chart
.ApplyCustomType ChartType:=xlUserDefined, TypeName:="Standard"

With argChart
.Height = 150
.Width = 150
.Top = 100
.Left = 100
End With
........
end sub

and the other one:

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

'loopar igenom de selekterade objekten. om ett objekt är en Chart så
'skickas det vidare till Sub linjeDiagramKnapp.
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
Set currentChart = obj
Call linjeDiagramKnapp(currentChart) 'skickar Chart
End If
Next
'slut på loop
End Sub

Sub linjeDiagramKnapp(argChart As Object)

Dim mySize As Double
With argChart.Chart
'här börjar inställningen för chart
.ApplyCustomType ChartType:=xlUserDefined, TypeName:="Standard"

With argChart
.Height = 150
.Width = 150
.Top = 100
.Left = 100
End With
......
end sub.

The one called stapeldiagram gives me the error message "Argument not
optional" but it works if you go into the code and use "F8" to walk through
the lines.... Perhaps it is not possible to see in the code what is wrong but
I have selected the charts and I cannot figure out what is wrong. PLease help
me out! I know I am a pain but I really dont understand why i am having such
trouble with this...(perhaps because I really suck at coding I guess). I
would very much appreciate any help I can get! Thank you all very much for
all your help!!
 
A

Arne Hegefors

Hi Tom! The problem is that the error message comes directly after I have
pressed the button. I then must press OK on the error message and then
nothing is highlighted in the code. Therefore I cannot really detect where
the problem is. However I changed the code for the button to a direct call
instead of .onaction and then it worked. My code then looked like:

Set stapelDiagramKnapp = .Controls.Add(Type:=msoControlButton)
With stapelDiagramKnapp
.Caption = "Stapeldiagram"
...
end with
call Modul1.arrayLoop

that worked but that means that it runs every time the button is created and
that is not what i want. I just cannot understand what is wrong. As i said it
works fine when I skip the button and just use "F8" starting at the arrayLoop
sub so there must be something strange with the button. but it is the same
code for the two buttons ie I dont really understand where the difference is.
I do not know if this gives you a better understanding of the probelm but if
you have any tips, ideas please help me out! Thanks very much!

"Tom Ogilvy" skrev:
 
A

Arne Hegefors

Hi Tom! I changed the order in the code for the two buttons and then it
worked...Do not ask me why but as long as it works I am happy! Please accept
me thanks for all your help, I know I have been a oain but I just did not get
it! Again tahnk you very much!

"Arne Hegefors" skrev:
 
N

NickHK

Arne,
You seem to have 2 With blocks active at the same time.
1 - Set stapelDiagramKnapp = .Controls.Add(Type:=msoControlButton)
Which presumably referes to a CommnadBar object.
2 - With stapelDiagramKnapp 'Button
.Caption = "Stapeldiagram"

Whilst the compiler may be able to work this out, it may be erratic.

NickHK
 

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

Similar Threads

tag different modules 4
Chart syntax 4
Selection with one object 3
Realtime chart example 2

Top