Excel and VBA

KenHigg

Registered User
Local time
Yesterday, 21:50
Joined
Jun 9, 2004
Messages
13,327
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.

???
 
Last edited:
Search on automation, which is the basic technique used. The relevant part:

xl.Rows("1:2").Select
xl.Selection.entirerow.Delete
 
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.

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

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

set xlWrkBk=xlApp.workbooks.open("Path")
[/CODE]
 
Thanks Paul and Keith - Just the kickstart I needed.

Here's the code I used:

Code:
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
 

Users who are viewing this thread

Back
Top Bottom