Chaange event to work on every sheet

im_kaushal

New Member
Joined
May 8, 2010
Messages
27
Hi All,

The below mentioned event code changes the changed cell value to bold. I want this to be applicable to all the worksheets available in the workbook. I know i can paste this code in all the worksheets, but just want to know is there a better way of making it applicable in all the worksheets, specially in a situation where my workbook has around 50 sheets.

macro which changes the cell value to bold is

Private Sub Worksheet_Change(ByVal Target As Range)
Target.Font.Bold = True
End Sub

Regards, Kaushal
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
This should work

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Stop
End Sub
 
Upvote 0
Rasm, I guess you meant to use the SheetChange event and not the SheetSelectionChange event.


Kaushal, paste in the ThisWorkbook module:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.Font.Bold = True
End Sub
 
Upvote 0
Thanks everyone it works absolutely fine..

Can somebody please explain how this event work for my understanding

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

I need to understand ByVal, Sh , etc. what does this means ?


Many thanks in advance
Regards, Kaushal
 
Upvote 0
you can set a variable to be ByRef or ByVal - means if they can be changed inside the sub/function.

The varibles passed to the sub/function can be read by you - i.e. a target will tell which cell is active i.e. Target.adress returns someting like "A1" (if you clicked cell A1 or Target.row would return 1.

You should insert a breakpoint into the code (press inside the narrow grey bar next to the code - bar is vertical) - also turn on i.e a Local watch point - now you can see the value of all the varaibles applicable to that sub/function when the program stops at the breakpoint.
 
Upvote 0
Thanks everyone it works absolutely fine..

Can somebody please explain how this event work for my understanding

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

I need to understand ByVal, Sh , etc. what does this means ?


Many thanks in advance
Regards, Kaushal

Sh is a variable representing the worksheet
Target represents the selected cell(s) on that sheet
When you use parameters in a sub/function you can pass them ByRef (the default) or ByVal.

Paste this into a new module and run Procedure1, to see what ByRef does to the 4th message. Then change to ByVal and run it again.

Code:
Sub Procedure1()
    Dim strMessage As String
    
    strMessage = "Born in Proc1"
    
    MsgBox "1: " & strMessage
    
    Procedure2 strMessage
    
    MsgBox "4: " & strMessage
End Sub

Sub Procedure2(ByRef strMessage As String)
    MsgBox "2: " & strMessage
    
    strMessage = "Changed in Proc2"
    
    MsgBox "3: " & strMessage
End Sub
Notice that when you use ByVal, you don't alter the variable in the *original* procedure. Using ByRef you do, because ByRef passes the original memory address of the variable; ByVal only passes the value.

Denis
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top