Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing Sheet events in Excel 1

Status
Not open for further replies.

SunnyShine

Structural
Jan 25, 2000
17
0
0
IN
Writing sheet events in Excel is very simple. Just right click in sheet tab and select view code. Select work sheet from objects section and apropriate event from the declarations section. Write necessary code. Thats all. You can do a lot with this feature. You can change the back ground color of cell with a single click in a cell or can change the back color when the value of cell falls with in a certain limit for a warning in your calculations.
 
You are certainly right. This stuff is great! Heres an example of how you can use it. We have spreadsheets that are used by Americans and Europeans and wanted them to be divided into an English unit side and a Metric unit side. (The actual calculations can be in the cells on either side.) It's easy to make the outputs in both English and Metric simply by converting the final calculation outputs cell over to the other unit with function in cells on it's respective side, but we wanted the input to be in either English or Metric with automatic, dynamically updated unit conversion to the other side. There are probably other ways of doing this, but Sheet Event Programming did it seamlessly. Here's an example (The variables in square brackets are named input cells, those that end in an "M" are on the Metric side of the speadsheet.)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Application.ScreenUpdating = False
On Error GoTo ExitSub
Select Case Target.Name.Name
Case Is = "Dia"
[DiaM] = [Dia] * 2.54
Case Is = "DiaM"
[Dia] = [DiaM] / 2.54
Case Is = "H"
[HM] = [H] * 2.54
Case Is = "HM"
[H] = [HM] / 2.54
Case Is = "Temp"
[TempM] = ([Temp] - 32) * (5 / 9)
Case Is = "TempM"
[Temp] = ([TempM] * (9 / 5)) + 32
End Select
ExitSub:
End Sub
 
Update on using the "Worksheet_Change" event. Below I have corrected the sample program I submitted earlier. Note that I have added two lines. These are necessary to keep the program from initiating itself each time it changes the worksheet. (Without these changes, the program still works, but it runs much longer than it needs to.)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Example Worksheet Event Code to convert between Metric and English units in input cells
' CAUTION delete the "t" in front of the subroutine name.
Application.ScreenUpdating = False
Application.EnableEvents = False'<<<<<CHANGE
On Error GoTo ExitSub
Select Case Target.Name.Name
Case Is = &quot;MdotE&quot;
[MdotM] = [MdotE] / 2.204623 'lbm/hr >> kg/hr
Case Is = &quot;MdotM&quot;
[MdotE] = [MdotM] * 2.204623 'kg/hr >> lbm/hr
Case Is = &quot;DiaE&quot;
[DiaM] = [DiaE] * 2.54 'inches >> cm
Case Is = &quot;DiaM&quot;
[DiaE] = [DiaM] / 2.54 'cm >> inches
Case Is = &quot;PressE&quot;
[PressM] = [PressE] * 6894.757 'psia >> Pascals
Case Is = &quot;PressM&quot;
[PressE] = [PressM] / 6894.757 'Pa >> psia
Case Is = &quot;TempE&quot;
[TempM] = ([TempE] - 32) * (5 / 9) 'F >> C
Case Is = &quot;TempM&quot;
[TempE] = ([TempM] * (9 / 5)) + 32 'C >> F
End Select
ExitSub:
Application.EnableEvents = True '<<<<<CHANGE
End Sub
 
Status
Not open for further replies.
Back
Top