Excel VBA - Multiple buttons pointing to same event - excel

I am currently working on some excel document to automate it as much as possible.
For this I have to add the same button (let's call it an Erase All button for example) in each of the sheets.
Till here is OK. I have added it and all works fine. The problem is that the onClick event code is replicated in all the different sheets as a new Microsoft Excel Object code. What I would like is all the buttons to point to a single Module so I dont have to replicate code anywhere.
I come from a .Net environment and I know that VBA may have some restrictions or different usage but wondering whether I can avoid the onClick event in every sheet and put this onClick on a single generic module.
As a workaround I added my functionality as a SUB in the module and I call it from each of the different onClick events but if it is possible to directly clean all the sheet-specific code then it would be great :)
Any clue on that?
Hope is enough clear but in case need some more description please let me know and will try to add some graph/pic
Thanks a lot in advance!!

You can assign the same macro to each of the buttons. (Just right click on the button and 'assign macro')
As long as you designate the ActiveWorksheet as the sheet to process within the macro, this should be fine.
Alternatively you could instead create a non-modal userform with the required generic buttons which then has code to work on the active worksheet when pressed

Related

Fill in a textbox according to checkbox and option selection (don't even know if i'm creating it the best way

On a userform I am trying to populate a textbox with cell value according to a checkbox and optionbutton combination.
The bigger picture is i'm trying to create a macro that will automatically create emails.
Firstly I have 7 checkboxes (Applications) , one could be ticked or a couple could be ticked. Then it is the type of email I would like to create, for this I have used optionbuttons.
Not necessarily after code (though that would be helpful) but just need some guidance on how to code it as I have written and then re-written so much code and the last one was way over the top for what I needed.
Any advice much appreciated

Can´t find my code in excel vba

I have written a code that should run when a cell in the worksheet changes. It works well and fast.
Now I want to add another case to the code... but now I can´t find it.
Can I somehow search for the code? There are no modules or forms and I have tried to select worksheet and change.. but the code does not appear.
Please help
Regards
Henrik
If the code reacts on Worksheet change, it's within Microsoft Excel Objects for the sheet
I don't know if this will help, but when my code hides (hugely frustrating!), I hit the "Macros" button - it should show up in the list there if it's working in your sheet. Then you can select it, choose "step into", and at least get to where you can edit the code. Good luck!

Excel - Disabel ability to manually change sheets (but don't hide them)

Apologies if this is a really stupid question but I've not been able to find a solution.
I've got a main page on my Excel workbook with a number of filter options and some buttons linked to vba code. When clicking the button it takes you another tab with a load of data filtered appropritately. There is a button there that removes the filters and takes you back.
This all works fine but what I want to do is stop people being able to manually change sheets but clicking on the tabs at the bottom of the screen. So far the only method I've found is hiding them, but this obviously doesn't work as you then can't see that data.
You can disable the editing of the sheet tab names by turning them off on the excel options as mentioned in the comment above. The downside to this approach is the user won't be able to see what sheet they are on (which I find incredibly frustrating).
The better method in my mind is to set up an event listener for when the user changes the name of the tab. You can then send a pop up box that appears when the user attempts to change the tab name.

Most User Friendly Way to use a VBA EXCEL Userform?

So I have programmed several userform with several user inputs(text boxes, check boxes, etc.) Now how can I use them outside of excel?
Currently I have to:
Open up the spreadsheet
Enable macros
Go to Developer Tab, View Code
Then find the right userform and run it or type F5.
Is there a more user-friendly way to run the userform? Its for testers who don't know vba or excel programming.
Thanks!
Yes, absolutely. You could create a button named after the form in question, and to that button assign the following macro:
Sub btn1()
frm1.Show
End sub
That's it! Of course, change the names according to what you have :)
I also reccomend having a button to close the form on the form itself.
OF COURSE this still uses Excel. If you want to use them on their own you could have something like this guy did: How to Open only UserForm of an excel macro from batch file

events and macros in excel vba not linking

I have an existing excel sheet which has some macros written in modules. The sheet has various button kind of controls (check 1st scenario), but not a button (as I could not see the button control selected in even in design mode, as it is in the case of 2nd scenario).
So I was wondering what control it is and how can I link it with macros defined?? Also the coding is in modules and not in sheet, why??
I even tried to link a macro with the button created in scenario 2, but it didnt work out. I am new to excel vba, pls suggest some solution.
Also I would like to know more about macro, like why do we need a macro if we can write event based code on button click??
First off "Macro" is the generic term excel uses for VBA code. VBA is the language you are writing in when you "writing a macro". You can write VBA in a subroutine as part of the sheet object, workbook object, an independent module, class, or form. Events are part of a workbook, worksheet, or form object that can trigger code that is contained within the event's VBA routine.
Your Scenario 1 looks like maybe those are shapes drawn on the worksheet. Shapes can have "macros" assigned to them. You right click on the shape, and go to "Assign Macro". It will write the event subroutine in that worksheet's code in your Visual Basic Editor (VBE). You can then write the code inside that subroutine that was generated for you which may call other subroutines or functions that you have written in a module.
You can also tie subroutines, regardless of which object they are written in (module, worksheet, workbook) to a CommandButton, like in your second scenario. This particular button you added is an ActiveX control type button. They tend to be a bit more complicated. You should probably just go with a normal "Form Control" button as it will ask you, after you draw it, which SubRoutine you want to attach it to.
So to answer you question: Go to Developer>>Insert>>Form Control>>Button and draw it on. Then point it to your VBA subroutine that has the code you want to execute when you hit the button. Nice and simple.

Resources