Macro Code Help for multiple worksheets.

Gajendran Yadhav

Board Regular
Joined
Sep 8, 2023
Messages
51
Office Version
  1. 2010
  2. 2007
Platform
  1. 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)

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
1707975716806.png


any help will be appreciated...

TIA.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
When you place this code into the 'ThisWorkbook' module you don't need to run it. Make sure you are in a sheet that is named in your case statement (i'e. a sheet named 'AP') and then make a change to a value in column P and it will run by itself in the same way it did at sheet level.

You don't need that line 'Dim ws As Worksheet'

If it still won't run then maybe you have EnableEvents stuck on false, if this is the case try running the below in a standard module and then try again:
VBA Code:
Sub test()
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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