home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Begin VB.Form ExcelVBA
- Caption = "Excel VBA Demo"
- ClientHeight = 3855
- ClientLeft = 60
- ClientTop = 345
- ClientWidth = 8070
- LinkTopic = "Form1"
- ScaleHeight = 3855
- ScaleWidth = 8070
- StartUpPosition = 3 'Windows Default
- Begin VB.TextBox Text1
- BeginProperty Font
- Name = "Courier New"
- Size = 9
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 3000
- Left = 105
- MultiLine = -1 'True
- ScrollBars = 3 'Both
- TabIndex = 3
- Top = 105
- Width = 7860
- End
- Begin VB.CommandButton bttnCalculate
- Caption = "Calculate Expression"
- BeginProperty Font
- Name = "Verdana"
- Size = 9.75
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 555
- Left = 2460
- TabIndex = 2
- Top = 3225
- Width = 2220
- End
- Begin VB.CommandButton bttnExit
- Caption = "Exit Demo"
- BeginProperty Font
- Name = "Verdana"
- Size = 9.75
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 555
- Left = 5745
- TabIndex = 1
- Top = 3225
- Width = 2220
- End
- Begin VB.CommandButton bttnNew
- Caption = "Make New Sheet"
- BeginProperty Font
- Name = "Verdana"
- Size = 9.75
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 555
- Left = 135
- TabIndex = 0
- Top = 3225
- Width = 2220
- End
- Attribute VB_Name = "ExcelVBA"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- ' ******************************
- ' ******************************
- ' ** MASTERING VB6 **
- ' ** by Evangelos Petroutos **
- ' ** SYBEX, 1998 **
- ' ******************************
- ' ******************************
- Dim AppExcel As Excel.Application
- Private Sub bttnNew_Click()
- StartExcel
- MakeSheet
- AppExcel.Range("A2:E3").Select
- Set CData = AppExcel.Selection
- For icol = 1 To 5
- For irow = 1 To 2
- Text1.Text = Text1.Text & Chr(9) & CData(irow, icol)
- Next
- Text1.Text = Text1.Text & vbCrLf
- Next
-
- PrintSheet
- SaveSheet
- TerminateExcel
- End Sub
- Sub StartExcel()
- Screen.MousePointer = vbHourglass
- Set AppExcel = CreateObject("Excel.Application")
- Screen.MousePointer = vbDefault
- End Sub
- Sub MakeSheet()
- Dim wSheet As Worksheet
- Dim wBook As Workbook
- Set wBook = AppExcel.Workbooks.Add
- Set wSheet = AppExcel.Sheets(1)
- wSheet.Cells(2, 1).Value = "1st Quarter"
- wSheet.Cells(2, 2).Value = "2nd Quarter"
- wSheet.Cells(2, 3).Value = "3rd Quarter"
- wSheet.Cells(2, 4).Value = "4th Quarter"
- wSheet.Cells(2, 5).Value = "Year Total"
- wSheet.Cells(3, 1).Value = 123.45
- wSheet.Cells(3, 2).Value = 435.56
- wSheet.Cells(3, 3).Value = 376.25
- wSheet.Cells(3, 4).Value = 425.75
- ' Format column Headings
- Range("A2:E2").Select
- With Selection.Font
- .Name = "Verdana"
- .FontStyle = "Bold"
- .Size = 12
- End With
- Range("A2:E2").Select
- Selection.Columns.AutoFit
- Selection.ColumnWidth = Selection.ColumnWidth * 1.25
- Range("A2:E2").Select
- With Selection
- .HorizontalAlignment = xlCenter
- End With
- ' Format numbers
- Range("A3:E3").Select
- With Selection.Font
- .Name = "Verdana"
- .FontStyle = "Regular"
- .Size = 11
- End With
- wSheet.Cells(3, 5).Value = "=Sum(A3:D3)"
- MsgBox "The year total is " & wSheet.Cells(3, 5).Value
- End Sub
- Sub SaveSheet()
- AppExcel.AlertBeforeOverwriting = False
- On Error Resume Next
- AppExcel.Sheets(1).SaveAs FileName:="c:\sales.xls"
- End Sub
- Sub PrintSheet()
- AppExcel.ActiveWorkbook.PrintOut
- End Sub
- Sub TerminateExcel()
- AppExcel.ActiveWorkbook.Close False
- AppExcel.Quit
- Set AppExcel = Nothing
- End Sub
- Private Sub bttnExit_Click()
- End
- End Sub
- Private Sub bttnCalculate_Click()
- Dim wSheet As Worksheet
- Dim wBook As Workbook
- Dim expression
- StartExcel
- expression = InputBox("Enter math expression to evaluate (i.e., 1/cos(3.45)*log(19.004)")
- On Error GoTo CalcError
- If Trim(expression) <> "" Then
- MsgBox AppExcel.Evaluate(expression)
- End If
- GoTo Terminate
- Exit Sub
- CalcError:
- MsgBox "Excel returned the following error: " & vbCrLf & Err.Description
- Terminate:
- AppExcel.Quit
- Set AppExcel = Nothing
- End Sub
- Private Sub Form_Terminate()
- Set AppExcel = Nothing
- End Sub
-