[Excel vba] Issue with macro assignment -> object

Solved
xjl Posted messages 232 Status Membre -  
 Anonymous user -
Hi,

I have a small assignment problem:

N = Right(Cells(row, 2).Value, 3)
Sheets(Cells(row, 1).Value).Shapes(Cells(row, 2).Value).OnAction = "SELECTION_TRONCON(N)"

Debugging gives me a 1004 error: This formula is too complicated to be assigned to an object

Actually, I have a macro that needs to assign another macro (SELECTION_TRONCON(N)) to an object based on the section number contained at the end of the cell (row, 2).

Does anyone know how I can modify the syntax to make it work, please?

Thanks in advance
Configuration: Windows XP Internet Explorer 6.0

6 réponses

Anonymous user
 
Hello,

a syntax story!

The line:
Sheets(Cells(line, 1).Value).Shapes(Cells(line, 2).Value).OnAction = "SELECTION_TRONCON(N)"

should read:
Sheets(Cells(line, 1).Value).Shapes(Cells(line, 2).Value).OnAction = "'SELECTION_TRONCON" & "(" & """" & N & """" & ")'"

Here is a functional example in my routine:

'Creating the command string for the VBA property <.OnAction>
CommandString = "'CreationPageBarreOutils " & "(" & """" & PageNum & """" & ")'"
'Assigning the command string to the VBA property <.OnAction>
Application.CommandBars("Block").Controls(Loop).OnAction = CommandString

Good luck

Lupin.
3
HKLM
 
Hi,

I would like to do the same thing with 2 parameters but it's not working. I don't really understand why. It works with 1 but not with 2. Is it possible? Could you write the command line for me please? I must admit I'm a bit lost with all these quotes. Any additional explanation is welcome :-)

Thanks in advance
0
Anonymous user > HKLM
 
re :

Here, the command chain should look like this:

 Sub Test() Dim Chaine As String Dim N As Long, J As Long N = 2: J = 5 Chaine = "'SELECTION_TRONCON" & "(" & N & "," & J & ")'" MsgBox Chaine End Sub ' 


The name of the function, including parameters, must first be enclosed in single quotes (quote)
and the whole thing in double quotes.

That's why the syntax starts like this:

Chaine = " ' SEL...


Lupin
0
HKLM > Anonymous user
 
Thank you Lupin,
Your solution works well with the MsgBox instruction, but when I try to assign the string to a button via .OnAction, it doesn't work and I get "Could not find the macro." I imagine I need to add some series of quotes somewhere, but I'm not quite sure where?

Here is the code:

Sub ...

H As Integer
W As Integer

...

.OnAction = "affectemacro(" & H & "," & W & ")"

End Sub

I don't remember what to do to achieve this. Thank you for your help.
0
Anonymous user > HKLM
 
re:

Is the macro you are calling [ affectemacro ] in another module?
Is this macro declared as [ Private ]?

Otherwise, you would need to paste the complete instruction so that I can
make it work. I discovered this syntax in my book a few
years ago, but I only used one argument.

Given that a function or routine can have more than one
argument, it is clear that it should work.

Lupin
0
HKLM > Anonymous user
 
To be brief, here is the logic of the code.

I have a macro that performs various calculations based on a range of data (Range) and an integer.

I need to be able, via a button, to duplicate the table containing the data and to add a button for each new occurrence to execute the previous macro (calculations)


Here is the code in question

The compute macro

Public Sub compute_delivery(my_cells As Range, market As Integer)

Dim...

Line1 = Line2 + 1 'formulas are actually more complicated in reality
Line2 = Line3 * 1/2

...

End Sub

The part adding a button (the tricky part):

Sub Add_button_w_macro(PositionCell As Range, Txt As String, Data_macro As Range)
Const H As Integer = 51
Const W As Integer = 100
'
'
With ActiveSheet.Shapes.AddShape(msoShapeActionButtonCustom, PositionCell.Left + 6, PositionCell.Top + PositionCell.Height + 6, W, H)
With .Fill
.ForeColor.RGB = RGB(37, 64, 97)
.TwoColorGradient msoGradientHorizontal, 1
End With

With .TextFrame
.MarginBottom = 5
.MarginLeft = 5
.MarginRight = 5
.MarginTop = 5
.Characters.Text = Txt
'"Click here to" & Chr(10) & "realize schedule" & Chr(10) & " -MPR- "
.Characters.Font.Color = RGB(0, 0, 0)
.Characters.Font.Bold = True
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
.OnAction = "'compute_delivery " & "(" & """" & Data_macro & """" & ")'" 'works well with a parameter
.OnAction = "'compute_delivery (""Data_macro"",""1"")'" 'does not work with a 2nd

End With
End Sub
0
masterseb
 
Hi xlj,
I think you could use the Select Case method in your macro to determine the cases (value in row 2). Based on these values, you would call the relevant macro using the Call method + macro name.

Regards

Seb
See you later
0
xjl Posted messages 232 Status Membre 183
 
Indeed, it's possible; the only problem is that my macro assigns a macro to shapes, and I have a few hundred of them, meaning that if I use this method, I will need one macro per shape, whereas I would like to assign the same macro to all shapes that would take the end of the shape's name as a variable... I'm not sure if that's very clear?!

Thanks anyway, and any new ideas will be welcome! ;-)
0
masterseb > xjl Posted messages 232 Status Membre
 
Hi,
I believe I’ve partially understood your problem. I made an example that might help you. It takes 2 minutes to create.

1) In an Excel sheet, draw 2 rectangles. In the top left, you’ll have Rectangle 1, and Rectangle 2 in the name box next to the text box.
2) In cell A1, enter 1
3) Create the following macro:

Sub selectionshape ()

A = Cells(1, 1).value
ActiveSheet.Shapes("Rectangle " & A).Select

End Sub

4) Run your macro. If your cell A1 contains the value 1, your Rectangle 1 will be selected; if it contains the value 2, your Rectangle 2 will be selected.

This is just an idea; I hope it helps you. Let me know how it goes.
0
xjl Posted messages 232 Status Membre 183 > masterseb
 
Thank you, but it's not really what I need.

I need to have the same macro on the OnAction of my shapes, and that this macro is able to detect the name of the shape that was just clicked (with something like ActiveShape.Name, but that doesn't work) in order to assign the section number to the parameter N and then launch the macro SELECTION_TRONCON(N).

In fact, the main problem is that I don't know the syntax that can return the name of the shape that was clicked, for the rest it will be easy!

Thank you in advance for your ideas.
-1
masterseb
 
Hi xlj, I finally understood what you wanted, I did some tests and it works for me. In fact, error 1004 is generated by the variable (N) in your SELECTION_TRONCON macro because OnAction only accepts text within quotes.

So I tried the following modifications:

1) Declare your variable N as Public (Public N >>> at the very top)
2) For the OnAction, replace "SELECTION_TRONCON(N)" with "ShapeClick"
3) Create an intermediate macro called ShapeClick as follows:

Sub ShapeClick()

N = Right(Cells(row, 2).Value, 3)
Call SELECTION_TRONCON(N)

End Sub

To check, temporarily add this line in your SELECTION_TRONCON(N) macro:

j = Worksheets(1).Shape(N).Name 'adapt it to your code if necessary
msgbox j

Contact me if there’s a problem, I can assure you that when I click on a shape, it returns its name.

a+ seb
0
xjl Posted messages 232 Status Membre 183
 
Hi,

I have already tested something similar, but the only problem is that the line variable is not incremented here, so the cell where we will look for the name of the shape that was just clicked has nothing to do with that shape!

I can see how to run a macro from clicking on one of the shapes, but it seems more complicated to retrieve the name of the shape that just triggered the macro...

Thanks for your efforts!
0
xjl Posted messages 232 Status Membre 183
 
Hi,

I still haven't found a solution to this problem, so if anyone has an idea, I'm all ears...

Thanks in advance! ;-)
0
ChrisKang
 
I had the same problem as you, xjl, and I just found the solution right now:
From each of your shapes, you just need to call the same macro, without parameters.
Then, in this macro, you can retrieve the identity of the calling shape using the variable "Application.Caller".
If the Name property of your shapes is properly filled in, you should be able to call the correct parameterized macro from that.
Good luck, hoping this helps you as it did for me.

--
Chriskang
0
xjl Posted messages 232 Status Membre 183
 
Hi,

really well seen the trick of the Application.Caller that directly revises the shape's name!!

Thanks to you too Lupin, I'm sure your solution works too but it's a bit more complicated to set up...

Thanks everyone!
0
bourak
 
Hello, I have a problem. I double-click on the .xls files, but they don't open; I have to go through opening Excel and then file, open. Knowing that other files like *.doc open automatically with a double-click.
Thank you.
0