Gajendran Yadhav
Board Regular
- Joined
- Sep 8, 2023
- Messages
- 51
- Office Version
- 2010
- 2007
- Platform
- Windows
I have multiple sheets in a workbook. I have custom named all those. I want a specific macro to apply only to few of the custom named worksheets...
When i place the below code to each worksheet macro, this works very well. (auto clears the contents in the adjacent cells ( O & Q) then and there when any change is made in column P)
Since i need this to happen in almost 24 worksheets, i place it in all 24 worksheet macros. this makes the file size huge.
is there any possibility to have this code in a workbook module so that it does the work as intended...
when i tried it, the code seems not working. the modified code i wrote is..
when i manually tried running the macro, a dialog box opens, asking for the Macro Name:
the dialog box is
any help will be appreciated...
TIA.
When i place the below code to each worksheet macro, this works very well. (auto clears the contents in the adjacent cells ( O & Q) then and there when any change is made in column P)
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim cell As Range
' Set the range to the entire 16th column (Column P)
Set rng = Intersect(Target, Columns(16))
' Check if there are any changes in Column P
If Not rng Is Nothing Then
Application.EnableEvents = False ' Disable events to prevent infinite loop
' Loop through each changed cell in Column P
For Each cell In rng
' Clear the corresponding cells in Columns O (15th) and Q (17th)
cell.Offset(0, -1).ClearContents ' Clear contents of Column O (15th column)
cell.Offset(0, 1).ClearContents ' Clear contents of Column Q (17th column)
Next cell
Application.EnableEvents = True ' Enable events after the changes are made
End If
End Sub
Since i need this to happen in almost 24 worksheets, i place it in all 24 worksheet macros. this makes the file size huge.
is there any possibility to have this code in a workbook module so that it does the work as intended...
when i tried it, the code seems not working. the modified code i wrote is..
VBA Code:
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
' Check if the changed worksheet is one of the custom-named worksheets
Select Case Sh.Name
Case "AP", "AS", "BH", "BR", "CG", "Corp", "GJ", "JH", "KA", "KA2", _
"KL", "MH", "MP", "NB", "OD", "PB", "RJ", "SB", "TN", "TN2", _
"TS", "UK", "UP1", "UP2"
' Set the range to the entire 16th column (Column P)
Set rng = Intersect(Target, Sh.Columns(16))
' Check if there are any changes in Column P
If Not rng Is Nothing Then
Application.EnableEvents = False ' Disable events to prevent infinite loop
' Loop through each changed cell in Column P
For Each cell In rng
' Clear the corresponding cells in Columns O (15th) and Q (17th)
cell.Offset(0, -1).ClearContents ' Clear contents of Column O (15th column)
cell.Offset(0, 1).ClearContents ' Clear contents of Column Q (17th column)
Next cell
Application.EnableEvents = True ' Enable events after the changes are made
End If
End Select
End Sub
when i manually tried running the macro, a dialog box opens, asking for the Macro Name:
the dialog box is
any help will be appreciated...
TIA.