Try It Out Controlling an Excel Spreadsheet from Access

Vertex42 The Excel Nexus

Professional Excel Templates

Get Instant Access

In this example, you will create a new Excel workbook and populate some of the cells with data. 1. Add the following code to your module.

Sub ControlExcel()

Dim objExcel As New Excel.Application Dim objWorksheet As New Excel.Worksheet

'add a new workbook to the spreadsheet objExcel.Workbooks.Add

'point the worksheet variable at the active sheet Set objWorksheet = objExcel.ActiveSheet

'add various values to the cells

With objWorksheet

.Cells(1,

1)

Value =

"First Name"

.Cells(1,

2)

Value =

"Middle Initial"

.Cells(1,

3)

Value =

"Last Name"

.Cells(2,

1)

Value =

"John"

.Cells(2,

2)

Value =

"A."

.Cells(2,

3)

Value =

"Doe"

.Cells(3,

1)

Value =

"Jack"

.Cells(3,

2)

Value =

"D."

.Cells(3,

3)

Value =

"Smith"

End With

End With

'show Excel to the user objExcel.Visible = True

End Sub

2. Execute the ControlExcel procedure from the Immediate Window by typing ControlExcel and pressing Enter.

3. You should see an Excel screen similar to that shown in Figure 10.6.

Was this article helpful?

0 0

Post a comment