Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Writing Sheet events in Excel

Writing Sheet events in Excel

Writing Sheet events in Excel

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.

RE: Writing Sheet events in Excel

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
End Sub

RE: Writing Sheet events in Excel

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 = "MdotE"
                [MdotM] = [MdotE] / 2.204623       'lbm/hr >> kg/hr
            Case Is = "MdotM"
                [MdotE] = [MdotM] * 2.204623       'kg/hr >> lbm/hr
            Case Is = "DiaE"
                [DiaM] = [DiaE] * 2.54             'inches >> cm
            Case Is = "DiaM"
                [DiaE] = [DiaM] / 2.54             'cm >> inches
            Case Is = "PressE"
                [PressM] = [PressE] * 6894.757     'psia >> Pascals
            Case Is = "PressM"
                [PressE] = [PressM] / 6894.757     'Pa >> psia
             Case Is = "TempE"
                [TempM] = ([TempE] - 32) * (5 / 9) 'F >> C
            Case Is = "TempM"
                [TempE] = ([TempM] * (9 / 5)) + 32 'C >> F
        End Select
    Application.EnableEvents = True '<<<<<CHANGE
End Sub

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close