Allow Macro to Work by Selecting Cell Description

M

Maperalia

Could you please help me with this matter?

I have a three buttons with their respectively macros in one worksheet. The
macros names are Wood, Metal and Plastic.
In addition, I have a pull out list located in the cell A1 with the
following description:
Wood
Metal
Plastic

I would like to know if I can just run the macro’s name with its
respectively name pulled out in the list.

For example, I pick in the cell A1 Wood I just want the macro Wood be able
to run after I click it. If I click the button with the macro Metal and/or
the macro Plastic I want to get a window message “Pick the Wood Buttonâ€.

For example, I pick in the cell A1 Metal I just want the macro Metal be able
to run after I click it. If I click the button with the macro Wood and/or the
macro Plastic I want to get a window message “Pick the Metal Buttonâ€.

For example, I pick in the cell A1 Plastic I just want the macro Plastic be
able to run after I click it. If I click the button with the macro Wood
and/or the macro Metal I want to get a window message “Pick the Plastic
Buttonâ€.

Thanks in advance.
Maperalia
 
P

PA

There are several ways to do this. Here's two:

First:
Create a new function in the same module as your macros

Private Function ValidMaterialSelected(ByVal sMacroMaterial As String) As
Boolean
Dim sMaterialSelected As String

sMaterialSelected = Range("a1").Value

If sMacroMaterial = sMaterialSelected Then
ValidMaterialSelected = True
Else
MsgBox "Pick the " & sMaterialSelected & " button."
ValidMaterialSelected = False
End If
End Function

Then, at the beginning of each of your macros, add the following code:

if not ValidSelectedMaterial( [the name of your macro ie Wood, Metal,
Plastic]) then
exit sub
end if

Second way:

Create only one button on your sheet that would trigger the following code:

application.run range("a1")

This will launch the macro whose name is in A1.

By the way, I didn't test the code.

PA
 
P

Per Jessen

Hi

When a button is pressed, check if the corresponding text is selected in A1,
if not display message and exit sub. Otherwise continue code.

Private Sub cbPlastic_Click()
If Range("A1").Value <> "Plastic" Then
msg = MsgBox("Pick the " & Range("A1").Value & " button!",
vbExclamation, "Wrong Button")
Exit Sub
End If
'Your code
End Sub

Hopes this helps.
 
M

Maperalia

sPA;
Thanks very much. Your code runs excellent.

Maperalia

PA said:
There are several ways to do this. Here's two:

First:
Create a new function in the same module as your macros

Private Function ValidMaterialSelected(ByVal sMacroMaterial As String) As
Boolean
Dim sMaterialSelected As String

sMaterialSelected = Range("a1").Value

If sMacroMaterial = sMaterialSelected Then
ValidMaterialSelected = True
Else
MsgBox "Pick the " & sMaterialSelected & " button."
ValidMaterialSelected = False
End If
End Function

Then, at the beginning of each of your macros, add the following code:

if not ValidSelectedMaterial( [the name of your macro ie Wood, Metal,
Plastic]) then
exit sub
end if

Second way:

Create only one button on your sheet that would trigger the following code:

application.run range("a1")

This will launch the macro whose name is in A1.

By the way, I didn't test the code.

PA




Maperalia said:
Could you please help me with this matter?

I have a three buttons with their respectively macros in one worksheet. The
macros names are Wood, Metal and Plastic.
In addition, I have a pull out list located in the cell A1 with the
following description:
Wood
Metal
Plastic

I would like to know if I can just run the macro’s name with its
respectively name pulled out in the list.

For example, I pick in the cell A1 Wood I just want the macro Wood be able
to run after I click it. If I click the button with the macro Metal and/or
the macro Plastic I want to get a window message “Pick the Wood Buttonâ€.

For example, I pick in the cell A1 Metal I just want the macro Metal be able
to run after I click it. If I click the button with the macro Wood and/or the
macro Plastic I want to get a window message “Pick the Metal Buttonâ€.

For example, I pick in the cell A1 Plastic I just want the macro Plastic be
able to run after I click it. If I click the button with the macro Wood
and/or the macro Metal I want to get a window message “Pick the Plastic
Buttonâ€.

Thanks in advance.
Maperalia
 

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