miscellaneous topics isys562. scenario scenarios are part of a suite of commands sometimes called...

Post on 20-Dec-2015

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Miscellaneous Topics

ISYS562

Scenario

• Scenarios are part of a suite of commands sometimes called what-if-analysis tools. A scenario is a set of values that Microsoft Excel saves and can substitute automatically in your worksheet. You can use scenarios to forecast the outcome of a worksheet model. You can create and save different groups of values on a worksheet and then switch to any of these new scenarios to view different results.

Creating a Scenario

• Tools/Scenarios– Add scenario

• Changing cells• Resulting cells

• Demo: benefit.xls

Scenario Macro

ActiveSheet.Scenarios.Add Name:="GoUp", ChangingCells:=Range("C3:C7"), _ Values:=Array("350", "295", "1.7", "0.03", "0.062"), Comment:= _ "Created by cob on 11/30/2005", Locked:=True, Hidden:=False

ActiveSheet.Scenarios.Add Name:="GoDown", ChangingCells:=Range("C3:C7"), _ Values:=Array("230", "175", "1.7", "0.03", "0.062"), Comment:= _ "Created by cob on 11/30/2005", Locked:=True, Hidden:=False

Range("C23").Select

ActiveSheet.Scenarios.CreateSummary ReportType:=xlStandardSummary, _ ResultCells:=Range("C19:C23")

Entering Data Using Form

• Validating data

• Automatically move to the net row.

Code ExampleDim rowIndex, colIndex, KeepColIndex As IntegerPrivate Sub CommandButton1_Click()ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox1.TextcolIndex = colIndex + 1ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox2.TextcolIndex = colIndex + 1ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox3.TextcolIndex = colIndex + 1ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox4.TextTextBox1.Text = ""TextBox2.Text = ""TextBox3.Text = ""TextBox4.Text = ""colIndex = KeepColIndexrowIndex = rowIndex + 1End SubPrivate Sub RefEdit1_Change() rowIndex = Range(RefEdit1.Text).Row KeepColIndex = Range(RefEdit1.Text).Column colIndex = KeepColIndexEnd SubPrivate Sub UserForm_Initialize()'ActiveSheet.Cells(1, 1).Activate'rowIndex = ActiveCell.End(xlDown).Row + 1'colIndex = 1End Sub

Workbook’s SendMail Method

• Sends the workbook as attachment by using the installed mail system.

eid ename email bonuse1 peter dchao@sfsu.edu 500e2 paul dchao@sfsu.edu 1000e3 chao dchao@sfsu.edu 2000

Code Example

Sub SendAttachment()Dim i, LastRow As IntegerThisWorkbook.Sheets("SHEET1").Cells(2, 3).ActivateLastRow = ActiveCell.End(xlDown).RowFor i = 2 To LastRow ThisWorkbook.SendMail Cells(i, 3).Value, "Workbook attached"NextEnd Sub

Automation

• The process of one application’s controlling of another is called Automation or Object Linking and Embedding (OLE)

Send Email via Outlook

• Reference Outlook object

• Outlook mailItem object:– To, cc, bcc– Subject– Body– Send

Code ExampleSub sendEmail()Dim cell As RangeDim MyOutlook As Outlook.ApplicationSet MyOutlook = New Outlook.ApplicationDim MyMailItem As Outlook.MailItemDim i, LastRow As IntegerThisWorkbook.Sheets("SHEET1").Cells(2, 3).ActivateLastRow = ActiveCell.End(xlDown).RowFor i = 2 To LastRow Set MyMailItem = MyOutlook.CreateItem(olMailItem) With MyMailItem .To = Cells(i, 3).Value .Subject = "Your bonus" .Body = "your bonus is: " & Cells(i, 4).Value .Send End WithNextEnd Sub

Excel’s Events

• Workbook events:– Open, NewSheet, BeforeSave

• Worksheet events:– Change, SelectionChange, Calculate

• Chart events:– Select, SeriesChange

• UserForm events– Initialize, control events

• OnTime, OnKey

Example: Worksheet Change Event

• Occurs when cells on the worksheet are changed by the user.

• Private Sub Worksheet_Change(ByVal Target As Range)

• Target    The changed range. Can be more than one cell.

Code Example

Private Sub Worksheet_Change(ByVal Target as Range)

Target.Font.ColorIndex = 5

End Sub

Intersect Method• Returns a Range object that represents the

rectangular intersection of two or more ranges.• This example selects the intersection of two

named ranges, rg1 and rg2, on Sheet1. If the ranges don't intersect, the example displays a message.– Worksheets("Sheet1").Activate – Set isect = Application.Intersect(Range("rg1"), Range("rg2")) – If isect Is Nothing Then

• MsgBox "Ranges do not intersect"

– Else • isect.Select

– End If

Example of Change Event and Intersect Method

Private Sub Worksheet_Change(ByVal Target As Range)Dim cell As RangeIf Not Intersect(Range("InputArea"), Target) Is Nothing Then For Each cell In Intersect(Range("InputArea"), Target) If Not WorksheetFunction.IsNumber(cell.Value) Then MsgBox (cell.Address & " Is not numeric") Else If cell.Value > 100 Then MsgBox (cell.Address & " greater then 100") End If End If NextEnd IfEnd Sub

This example monitors and validates changes in a range named InputArea.

Application object’s OnTime Method

• Schedules a procedure to be run at a specified time in the future.

• This example runs my_Procedure 15 seconds from now:

– Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"

• This example runs my_Procedure at 5 P.M.:– Application.OnTime TimeValue("17:00:00"), "my_Procedure"

• Use Now + TimeValue(time) to schedule something to be run when a specific amount of time (counting from now) has elapsed. Use TimeValue(time) to schedule something to be run a specific time.

Splash Screen Example

Code Example

Private Sub Workbook_Open() UserForm1.ShowEnd Sub

1. Use ThisWork’s Open event to open the splashscreen.

2. Use the splash screen’s Initialize event to set the Timer.

Private Sub UserForm_Initialize() Application.OnTime Now + TimeValue("00:00:03"), "closeForm"End Sub

3. Use the CloseForm procedure in a Module to close the form.

Sub CloseForm() Unload UserForm1End Sub

Working with Text File

• Open pathName For Input/Output/Append• AS # fileNumber

File Input Example

Sub FileInput()Worksheets.AddOpen "c:\stdata.txt" For Input As #1Dim row As IntegerDim sid, sname As StringDim gpa As Doublerow = 1Do While Not EOF(1) Input #1, sid, sname, gpa Cells(row, 1).Value = sid Cells(row, 2).Value = sname Cells(row, 3).Value = gpa row = row + 1LoopClose #1End Sub

File Append Example

Sub FileAppend()Dim sid, sname As StringDim gpa As Doublesid = "s5"sname = "smith"gpa = 3.5Open "c:\stdata.txt" For Append As #1Write #1, sid, sname, gpaClose #1End Sub

Logging Workbook Usage

Private Sub Workbook_Open() Open "C:\exLog.txt" For Append As #1 Write #1, "Opened at " & Now Close #1End Sub

top related