ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Where to place code that changes a cell value? (https://www.excelbanter.com/excel-programming/353697-where-place-code-changes-cell-value.html)

George Furnell[_2_]

Where to place code that changes a cell value?
 
Good day,

I would like to know where to place my code, since placeing it in the module
section produces an error. The code is seen below. I need this code to
execute for the "current displayed" worksheet, but I do not believe that I
should copy it to ecery sheet, thats why I tried it in the modules.

Where should I put it to only have 1 location for the code?

Range("M5").Value = 5

I appreciate your help.

Kind regards
George

Jan Karel Pieterse

Where to place code that changes a cell value?
 
Hi George,

I would like to know where to place my code, since placeing it in the module
section produces an error. The code is seen below. I need this code to
execute for the "current displayed" worksheet, but I do not believe that I
should copy it to ecery sheet, thats why I tried it in the modules.

Where should I put it to only have 1 location for the code?


It depends on when exactly you want to execute this code. If it is by clicking
a toolbar button or selecting a menu item, a normal module is fine. If it has
to be done because of a certain event (cell changed, other selection made), the
best place for it depends on for what sheets the event has to be done. If for
just one, it goes in the code module of that sheet, using the
Worksheet_SelectionChange or Worksheet_Change event. Otherwise it should go in
the thisworkbook module, using the Workbook_SheetSelectionChange or the
Workbook_SheetChange events.

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com


Paul B

Where to place code that changes a cell value?
 
George, if you want to put 5 in the sheet every time you click on it try
this

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Range("M5").Value = 5
End Sub

To put in this macro, from your workbook right-click the workbook's icon and
pick View Code. This icon is to the left of the "File" menu this will open
the VBA editor, in Project Explorer double click on thisworkbook, under your
workbook name, if you don't see it press CTRL + r to open the Project
Explorer, then, then paste the code in the window that opens on the right
hand side, press Alt and Q to close this window and go back to your
workbook, now this will run every time you open the workbook. If you are
using excel 2000 or newer you may have to change the macro security
settings to get the macro to run. To change the security settings go to
tools, macro, security, security level and set it to medium

--
Paul B
Always backup your data before trying something new
Please post any response to the newsgroups so others can benefit from it
Feedback on answers is always appreciated!
Using Excel 2002 & 2003

"George Furnell" wrote in message
...
Good day,

I would like to know where to place my code, since placeing it in the

module
section produces an error. The code is seen below. I need this code to
execute for the "current displayed" worksheet, but I do not believe that I
should copy it to ecery sheet, thats why I tried it in the modules.

Where should I put it to only have 1 location for the code?

Range("M5").Value = 5

I appreciate your help.

Kind regards
George




George Furnell[_2_]

Where to place code that changes a cell value?
 
I would like the code to execute if the value of any cell changes, but I do
not want to copy the code to more than one location. I.E only one place to
update the code if maintenance is needed.

Regards
George

"Jan Karel Pieterse" wrote:

Hi George,

I would like to know where to place my code, since placeing it in the module
section produces an error. The code is seen below. I need this code to
execute for the "current displayed" worksheet, but I do not believe that I
should copy it to ecery sheet, thats why I tried it in the modules.

Where should I put it to only have 1 location for the code?


It depends on when exactly you want to execute this code. If it is by clicking
a toolbar button or selecting a menu item, a normal module is fine. If it has
to be done because of a certain event (cell changed, other selection made), the
best place for it depends on for what sheets the event has to be done. If for
just one, it goes in the code module of that sheet, using the
Worksheet_SelectionChange or Worksheet_Change event. Otherwise it should go in
the thisworkbook module, using the Workbook_SheetSelectionChange or the
Workbook_SheetChange events.

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com



Jan Karel Pieterse

Where to place code that changes a cell value?
 
Hi George,

I would like the code to execute if the value of any cell changes, but I do
not want to copy the code to more than one location. I.E only one place to
update the code if maintenance is needed.


IN that case you need the Workbook_SheetSelectionChange event in the
Thisworkbook module.

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com



All times are GMT +1. The time now is 06:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com