assigning macro object

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Assigning macro to object

Assigning macro to object

resolvedResolved · High Priority · Version 2016

Jonathan has attended:
Excel Intermediate course
Excel Advanced course
Excel VBA Introduction course

Assigning macro to object

First of all, thank you to Jens for a great day yesterday.
I thought the course contents was very comprehensive and I'm looking forward to reviewing a lot of the work we went over.

I just had a query relating to assigning a macro to an object.
I have created a button through activating the Developer tab, and then on the developer tab going insert button.

I want to create a workbook which has 2 worksheets. Each worksheet needs to ask a question. Each worksheet will have 2 buttons you can click.
on the first worksheet both buttons will take the user to the 2nd worksheet.
The second worksheet, however, I will again have 2 buttons, but I want Excel to remember where I clicked on the 1st worksheet, to take the user to 2x2 options (4 different options).
I hope that makes sense.
I believe that this requires a public variable.
If you could give some guidance on how this is achievable that would be appreciated.
Thanks again for the course yesterday.
Kind regards,
Jonny Bingham

RE: Assigning macro to object

Hi Jonny

Glad to hear you had a great day on Excel VBA Introduction!
Just to clarify clicking the buttons on worksheet 1 navigates to worksheet 2 and remembers which button was clicked. Then what do want to to happen when clicking the buttons on worksheet 2?

Thanks
Doug Dunn
STL MS Office Trainer




RE: Assigning macro to object

Hi Doug,
Thanks ever so much for getting back.
My first worksheet has 2 buttons. I have created a macro for these. The first button has text in it which says 'Residential' and the second has text which says 'Commercial'.

In terms of the code the buttons which reads 'Residential' says:

Sub Residential ()
Worksheets ("Question 2").Activate
End Sub

The button which reads 'Commercial' says

Sub Commercial ()
Worksheets ("Question 2").Activate
End Sub

On the second worksheet, I have a further question.
This says, which company do you currently use for your searches?
- 'ABC' (I am changing name here)
- 'BCD'

I have created 4 worksheets after these 2 questions which I want to take the user to, depending on which button they click on the second screen. I want Excel to store as a variable the response to Question 1, and the response to Question 2, to take the user to a unique worksheet. i.e. if user answers Residential to the first question, and BCD to the second, then they should be taken to a page set up for BCD residential.

The four variables I need to record as strings would be
1) ABC Resi
2) ABC Commercial
3) BCD Resi
4) BCD Commercial.

I hope that this clarifies things.
If you have any more reuqestions, please let me know.
Thanks
Jonny

Edited on Thu 28 Jul 2016, 12:54

RE: Assigning macro to object

Hi again

Thanks for giving more detail.

Here are the macros for the 4 buttons...

Option Explicit
Option Compare Text
Dim Resi As String
Dim Comm As String


Sub Residential()
Worksheets("Question 2").Activate
Resi = "Yes"
End Sub

Sub Commercial()
Worksheets("Question 2").Activate
Comm = "Yes"
End Sub

Sub ABC()
If Resi = "Yes" Then
Worksheets("ABC Residential").Activate
ElseIf Comm = "Yes" Then
Worksheets("ABC Commercial").Activate
End If
Resi = ""
Comm = ""
End Sub

Sub BCD()
If Resi = "Yes" Then
Worksheets("BCD Residential").Activate
ElseIf Comm = "Yes" Then
Worksheets("BCD Commercial").Activate
End If
Resi = ""
Comm = ""
End Sub


The option explicit is in case a variable is miss spelled and the option compare text is so the if statements are not case sensitive.

Let me know if it works!

Regards
Doug

RE: Assigning macro to object

Thanks Doug.
Worked a dream.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Using Excel's MODE function

Use Excel's MODE function to display the most common value present in a particular range of cells.

The Mode function looks like this:

=MODE(cell range)

As an example, if 35 is the most commonly recurring number in a particular cell range, then the function will display 35.

View all Excel hints and tips


Server loaded in 0.06 secs.