View Full Version : Excel and VBA


KenHigg
11-28-2007, 11:21 AM
How do you manipulate a spreadsheet with code in Access. Say I have spreadsheet where I need to delete the first two rows at the top.

???

pbaldy
11-28-2007, 11:58 AM
Search on automation, which is the basic technique used. The relevant part:

xl.Rows("1:2").Select
xl.Selection.entirerow.Delete

KeithG
11-28-2007, 12:01 PM
Something like below will open up your spreadsheet and you can manupliate it form there. Make sure to set a reference to the excel object library. I guess you could also use late binding instead.

First set a reference to the Excel object library. Then you can use something like below to open the spreadsheet.

Dim xlApp as New Excel.Application
Dim xlwrkBk as Excel.Workbook
dim xlSheet as Excel.Worksheet

set xlWrkBk=xlApp.workbooks.open("Path")

KenHigg
11-28-2007, 01:04 PM
Thanks Paul and Keith - Just the kickstart I needed.

Here's the code I used:

Private Sub Command0_Click()

Dim xlApp As New Excel.Application
Dim xlwrkBk As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlwrkBk = xlApp.Workbooks.Open("C:\Documents and Settings\340364\60 Project Files\140 Excel Automation\ZXOR27NovB.xls")

Set xlSheet = xlwrkBk.Worksheets("OpenExchangeOrders27Nov")

xlSheet.Rows(1).Delete

xlwrkBk.Save
xlwrkBk.Close

Set xlSheet = Nothing
Set xlwrkBk = Nothing

xlApp.Quit

Set xlApp = Nothing

MsgBox "Done"

End Sub