Solved

Confused Over Modules & Individual Sheet Code

Posted on 2013-01-10
5
261 Views
Last Modified: 2013-01-18
A lot of confusion on my part here .....

Should I keep all my code in modules or is good practice to keep some code within a sheet?

When should I use form controls (eg buttons) and when should I use ActiveX buttons?

When I create code by recording a macro, I'm pushed into using form controls? Can I change form controls to an activeX controls, or should I copy recorded code into a new procedure?

Specifically, what should I do with my Sub DeleteData() (some of which came from a macro recording) ?
Module CodeSheet Code
0
Comment
Question by:Eirman
  • 2
  • 2
5 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 300 total points
ID: 38764369
You should use (the IMO poorly named) form controls on a worksheet and ActiveX control on a Userform. You can however use ActiveX forms on a sheet but it's not recommended. If you do do that, put the code (or cut and paste it) in the button's Click event.

If the same code is executed from more than one place the code should be in a Public Sub or Function in a Module, otherwise it's okay in the sheet, workbook or userform.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38764794
Hi, Eirman.

Couple of points...
(1) Your macro shows symptoms of the Macro Recorder. The following two lines...
Range("B10:BK10").Select
Selection.ClearContents

...should be replaced by....
Range("B10:BK10").ClearContents
(2) It's often considered good practice, where possible, to put macros in Modules.

Regards,
Brian
0
 
LVL 23

Author Comment

by:Eirman
ID: 38766550
Thanks for the code simplification Brian.

I'm still not sure what to do regarding my specific query. I've got a two Buttons on the ESB-2 sheet....
One is a command button where I can use click or double click.
The other is a form button where I don't appear to have the same flexibility.
0
 
LVL 26

Assisted Solution

by:redmondb
redmondb earned 200 total points
ID: 38766924
Eirman,

The ActiveX button's code will be in the Sheet. The Form button can be in either place, but I would put it in a module.

(BTW, the code changes are not just a simplification - the main reason for them is that they are faster.)

Regards,
Brian.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 38793435
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question