Worksheet Events Workout
- 04:07
Worksheet Events Workout
Transcript
In this workout, we are required to write VBA code to select a sheet.
When a cell with the corresponding sheet name is double clicked, we're told the regional summary sheet contains cells with the names of two regions.
If cell six is double clicked, the east sheet tab should display and if cell B 7 is double clicked, the west sheet tab should display and we have to write our VBA event code in the regional summary sheet.
Let's take a look at the regional summary sheet.
In cell B 6 we have east and in cell B 7 we have West and we can see to the right of the regional summary sheet, there are two sheet tabs, the east sheet tab and the west sheet tab.
Let's go into the Visual basic editor and write our event code.
We are going to write our event code within the regional summary sheet.
This will be a worksheet event.
To get the code window for the regional summary sheet, go to the Project explorer and double click regional summary.
From the object dropdown, select worksheet and from the procedure dropdown select before double click.
We don't need this procedure, which is selection change, but what we do need is our procedure that is applied to the before double click event Here the target is the range variable ie, the cell range object.
That will be double clicked.
We want to turn off the usual action associated with double clicking.
A note. A Boolean variable can only take the values of true or false and we want to make cancel equal to true.
We want to turn off the usual action that is associated with double clicking a cell in a spreadsheet to enable our event procedure to run.
So the first thing that we'll write is we want that cancel parameter to be equal to true.
And then the target parameter, well of course the target parameter refers to a cell range.
The value in the target cell is used to select a sheet with that same name.
So we can write worksheets.
The name of the worksheet will be Target the value in the cell. That is double clicked dot value.
And once we've got that worksheet, we want to go ahead and select it.
Let's test this in Excel. Back in Excel.
Let's test our event procedure.
If we double click a cell, we are hoping to be taken to a worksheet with the title that is equal to the value in that cell.
Let's go ahead and double click sell B 6 that has the value east and we're taken to the worksheet that has the same name ie; East as the value of that cell.
Let's try with West.
We double click and we're taken to the worksheet that has the value of West as its name, the same as the value in the cell that we double clicked.