Worksheet Change Event

shawleigh17

Board Regular
Joined
Nov 16, 2007
Messages
79
Is there a way to write a Worksheet_SelectionChange(ByVal Target As Range) event in module after creating a sheet in VBA? I constantly delete a sheet, then repopulate it with a new one that is empty, but I need to add some code that happens if they should change a particular cell. It worked when I ran it on a worksheet without refreshing, but as soon as I cleared and repopulated the sheet, it was gone. Is there a way to preserve this?
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Colin Legg

MrExcel MVP, Like totally RAD man
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Much simpler would be just to have a hidden, empty worksheet template with the event handler code already written in it's class code module.

Rather than adding a new sheet, just copy the template and make the copy visible. Copying the template will copy the event handler code.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,079
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

Or use the Workbook_SheetChange event and check if it's the sheet you are interested in.
 

Colin Legg

MrExcel MVP, Like totally RAD man
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Rory's implicitly picked up on something there -

You say that you are using the Worksheet_SelectionChange event handler to monitor if a particular cell is changed. You should be using the Worksheet_Change event handler...
 

shawleigh17

Board Regular
Joined
Nov 16, 2007
Messages
79

ADVERTISEMENT

I tried doing that last week, but I couldn't figure out how to do it. I posted on here, but nobody saw it/ offered suggestions, so I was trying to find a way around it.

Here's the prior post:

I am trying to write code in a sheet whenever that sheet is changed, but I only want this sheet to be affected, and no others. This sheet is deleted/recreated with each run of the larger program, so I can't really have standing code in the sheet file. Is there a way to do this? I want the sheet to essentially do this: When I change a value in the "From Location" column (B), I want column A to update with the vlookup value.

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If (Sh = Workbooks("WAT_File_Existence_Test.xls").Sheets("Print_Sheet")) Then
        Workbooks.Open ("D:\Personal\O&S_Report.xls")
        Sheets("SKU_Change_Plan").Visible = True
        Sheets("SKU_Change_Plan").Unprotect ("fritoIPM")
        ActiveCell.Offset(0, -1).FormulaR1C1 = "l;l,"
        Dim O_S_Value As Boolean
        O_S_Value = ThisWorkbook.Sheets("Velocity").Range("A9")
        
        If O_S_Value = "True" Then
            Descrp = Application.WorksheetFunction.VLookup(ActiveCell.Value, Workbooks("O&S_Report.xls").Sheets("SKU_Change_Plan").Range("All_Data"), 4, False)
        Else
            Descrp = Application.WorksheetFunction.VLookup(ActiveCell.Value, Workbooks("O&S_Report.xls").Sheets("SKU_Change_Plan").Range("All_Data"), 4, False)
        End If
        
        Application.DisplayAlerts = False
        Workbooks("O&S_Report.xls").Activate
        Sheets("SKU_Change_Plan").Protect ("fritoIPM")
        ActiveWorkbook.SaveAs ("D:\Personal\O&S_Report.xls")
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
    
    End If

End Sub
 

shawleigh17

Board Regular
Joined
Nov 16, 2007
Messages
79
Oh, nevermind, it looks like those are two different things. I'll look into the Worksheet_Change event handler.
 

Colin Legg

MrExcel MVP, Like totally RAD man
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I think I have unintentionally confused you. :(

My point was that you said you were using
Worksheet_SelectionChange at the sheet level which would equate to Workbook_SheetSelectionChange at the workbook level.


Whereas, as Rory said, you should be using
Worksheet_Change at the sheet level which would equate to Workbook_SheetChange at the workbook level.


Hope that clarifies...
 

shawleigh17

Board Regular
Joined
Nov 16, 2007
Messages
79
Okay, how do I use the Workbook_SheetChange Event and check that I am on the right sheet?

I have tried, but I don't think that I am doing it right...
 

Watch MrExcel Video

Forum statistics

Threads
1,122,717
Messages
5,597,729
Members
414,169
Latest member
Preston_Cleric

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
Top