?
Solved

Confused Over Modules & Individual Sheet Code

Posted on 2013-01-10
5
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
Martin Liss earned 900 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 24

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 600 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 49

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

764 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