events and macros in excel vba not linking - excel

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.

Related

Call 'Move or Copy' Dialog

In EXCEL there is an on-board 'Move or Copy' dialog, which can be started by clicking right on one or more sheets. The advantage of this feature is that a little window (see picture) is opened, and the user can select the destination. I wanted to add this routine to an existing sub. I know how to copy a sheet using VBA, that's not a problem, I wanted the same little window, and I was wondering if I can use the on-board routine. I have tried recording, but the window for selecting the destination did not occur in the code. Of course, I can simply rebuilt it, but why reinventing the wheel?
Is there a way of calling the routine?
It's a built-in dialog:
application.Dialogs(xldialogworkbookmove).Show
You can also use xldialogworkbookcopy and you should basically get the same dialog but with the checkbox at the bottom checked.

Having a sheet that contains buttons for macros that run off of selections

Is it possible to use Selection to define data used in a macro button that is in another sheet.
I'm trying to make a template workbook that's first sheet is a bunch of macro buttons. I want to be able to select data in another sheet, click my button sheet, and click my button to run a macro on the data I have selected in the other sheet.
The problem that I'm running into is that sheets seem to have independent selections at the same time. So my macro always runs on the selection from the button sheet instead of the sheet I was on. Any thoughts on how I can make this work? The selection of the data needs to be dynamic, so I can't just say .Cells(1,1).value because it might not be the data I need.
Say there are two types of sheets:
a set of data-sheets
a single button-sheet which controls processing via macros tied to buttons
We code a single global range variable in a standard module. We code selection change event macros in all the data-sheets. Then:
We click on a data-sheet
We select cells on the data-sheet
the event macro on that data-sheet records the selection in the global variable
We click to get on the button-sheet
We click on a button
the button macro retrieves the global range
the button macro determines the sheet associated with the stored range (from the Parent Property) and also the cells on that sheet which we selected
the button macro processes the data
EDIT#1:
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Also just Google: Excel VBA Event Macro
(there are many examples in this forum, for example)
https://msdn.microsoft.com/en-us/VBA/Excel-VBA/articles/workbook-sheetselectionchange-event-excel
In this link is where I need to start. Every time that I change my selection it runs an event where I could maybe grab the cell values and store them in an array that will stay static when I change sheets. If I get it right I'll post it here. 5/31/2018
To Accomplish this I made rng a public variable and stored my selection from sheet2 in it. Then I used a click event in my buttons to to call the macros that I wanted to run on my range. Here are some screenshots of the codes in my modules and in my sheets. This answer is for anyone that finds this in the future.
https://imgur.com/a/DJDaQiM
The Biggest thing to take away from this is that Public Variables in vba only have to be declared once in any of your modules. NOT IN YOUR SHEET CODE. This was the biggest thing that held me back from getting this done. Good luck and contact me with any questions.

Excel VBA - Multiple buttons pointing to same event

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

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

Show a form on all worksheets in an Excel file

I am writing a little generator script (using Excel VBA) to generate a bunch of source code, given various input from users.
Currently I have one 'generator control' worksheet that provides access to the generation scripting tools. Here there is a button that pops up a little form with various generation options.
Each portion of source code also gets its own worksheet for generation purposes. So, I essentially have one worksheet for generation commands and perhaps 10-20 that are for source code parameters.
What I would like to do is somehow take my popup form and embed it within each other worksheet...make it 'always visible' in the corner or something like that.
Each source code worksheet is generated programmatically as well...I'd assume that I could replicate my popup button for each worksheet, but I'd rather have this form always visible.
Is something like this possible?
Rather than putting it on the worksheet, could you not have a toolbar button that simply pops up a dialog? (In other words, your user interface is a UserForm rather than a set of worksheet controls).

Resources