Excel Vba Sheet Selection Change
This opens the code window for that sheet object.
Excel vba sheet selection change. Use the visual basic editor in the project explorer double click on the appropriate sheet under microsoft excel objects which is under the vbaproject name of your workbook in the code window select worksheet from the left side general drop down menu and then select selectionchange from the. The following will colour cell b2 red whenever the cell changes. More activate select sheet examples. Select worksheet by index number.
Private sub worksheet selectionchange byval target as range with activewindow scrollrow target row scrollcolumn target column end with end sub. The worksheet selectionchange event procedure executes when a cell is selected. To create a worksheet selectionchange event. This example scrolls through the workbook window until the selection is in the upper left corner of the window.
In the visual basic editor you must first double click the sheet name where the cell changes that activates the macro. The following code highlights the active cell with a red color every time a different cell is selected. Set activesheet to variable. In this case i wanted to run a macro when a cell in sheet1 changes.
This example displays the sheet name and address of the selected range in the status bar. I will start out by showing how a change to a single cell can trigger an action. The selection change event occurs when the selection changes on a worksheet either by the user or by any vba application. Vba coding made easy.
Have questions or feedback about office vba or this documentation. Private sub workbook sheetselectionchange byval sh as object byval target as excel range application statusbar sh name target address end sub support and feedback. The following uses the byval target as range line which uses the variable named target. After opening the code window for the worksheet you place your code in the worksheet change event.
In excel a worksheet change event is a trigger for a macro when a cell or group of cells change. Loop through selected sheets. Worksheet selectionchange procedure is installed with the worksheet ie. Select worksheet by tab name.
Private sub worksheet selectionchange byval target as range cells interior colorindex 0 target interior colorindex 3 end sub.