Download presentation
Presentation is loading. Please wait.
1
Miscellaneous Topics ISYS562
2
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.
3
Creating a Scenario Tools/Scenarios –Add scenario Changing cells Resulting cells Demo: benefit.xls
4
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")
5
Entering Data Using Form Validating data Automatically move to the net row.
6
Code Example Dim rowIndex, colIndex, KeepColIndex As Integer Private Sub CommandButton1_Click() ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox1.Text colIndex = colIndex + 1 ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox2.Text colIndex = colIndex + 1 ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox3.Text colIndex = colIndex + 1 ActiveSheet.Cells(rowIndex, colIndex).Value = TextBox4.Text TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" colIndex = KeepColIndex rowIndex = rowIndex + 1 End Sub Private Sub RefEdit1_Change() rowIndex = Range(RefEdit1.Text).Row KeepColIndex = Range(RefEdit1.Text).Column colIndex = KeepColIndex End Sub Private Sub UserForm_Initialize() 'ActiveSheet.Cells(1, 1).Activate 'rowIndex = ActiveCell.End(xlDown).Row + 1 'colIndex = 1 End Sub
7
Workbook’s SendMail Method Sends the workbook as attachment by using the installed mail system.
8
Code Example Sub SendAttachment() Dim i, LastRow As Integer ThisWorkbook.Sheets("SHEET1").Cells(2, 3).Activate LastRow = ActiveCell.End(xlDown).Row For i = 2 To LastRow ThisWorkbook.SendMail Cells(i, 3).Value, "Workbook attached" Next End Sub
9
Automation The process of one application’s controlling of another is called Automation or Object Linking and Embedding (OLE)
10
Send Email via Outlook Reference Outlook object Outlook mailItem object: –To, cc, bcc –Subject –Body –Send
11
Code Example Sub sendEmail() Dim cell As Range Dim MyOutlook As Outlook.Application Set MyOutlook = New Outlook.Application Dim MyMailItem As Outlook.MailItem Dim i, LastRow As Integer ThisWorkbook.Sheets("SHEET1").Cells(2, 3).Activate LastRow = ActiveCell.End(xlDown).Row For 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 With Next End Sub
12
Excel’s Events Workbook events: –Open, NewSheet, BeforeSave Worksheet events: –Change, SelectionChange, Calculate Chart events: –Select, SeriesChange UserForm events –Initialize, control events OnTime, OnKey
13
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.
14
Code Example Private Sub Worksheet_Change(ByVal Target as Range) Target.Font.ColorIndex = 5 End Sub
15
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
16
Example of Change Event and Intersect Method Private Sub Worksheet_Change(ByVal Target As Range) Dim cell As Range If 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 Next End If End Sub This example monitors and validates changes in a range named InputArea.
17
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.
18
Splash Screen Example
19
Code Example Private Sub Workbook_Open() UserForm1.Show End 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 UserForm1 End Sub
20
Working with Text File Open pathName For Input/Output/Append AS # fileNumber
21
File Input Example Sub FileInput() Worksheets.Add Open "c:\stdata.txt" For Input As #1 Dim row As Integer Dim sid, sname As String Dim gpa As Double row = 1 Do 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 + 1 Loop Close #1 End Sub
22
File Append Example Sub FileAppend() Dim sid, sname As String Dim gpa As Double sid = "s5" sname = "smith" gpa = 3.5 Open "c:\stdata.txt" For Append As #1 Write #1, sid, sname, gpa Close #1 End Sub
23
Logging Workbook Usage Private Sub Workbook_Open() Open "C:\exLog.txt" For Append As #1 Write #1, "Opened at " & Now Close #1 End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.