简体   繁体   中英

Save changes in an Excel spreadsheet that were made using MS Access VBA

In my database, I have a VBA script that opens an Excel spreadsheet and removes the first row so the new first row is the header row.

When I save the file in Access VBA and open the file in Excel, nothing is there.

If I reimport the spreadsheet, the data is visible.

I changed my script around and if I don't save the changes the file is ok. If I save the changes then this problem appears.

dim sheetpath as string
dim xl as excel.application
dim xlbook as excel.workbook
dim xlsheet as excel.worksheet

sheetpath = "c:\users\me\export.xlsx"

set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)

xl.visible = true
set xlsheet = xlbook.Worksheets(1)

If xlsheet.Range("a1").mergecells = true then
    xlsheet.cells.unmerge
end if

if xlsheet.range("a1") = "Values" then
    xlsheet.rows(1).delete
end if

xlbook.close savechanges:=true
xl.application.qit
set xl = nothing
set xlbook = nothing
set xlsheet = nothing

Try a reboot.

But before you do that, check in Task Manager / Process Explorer, if you have multiple Excel processes still running.

Instead of

set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)

do

Set xlbook = GetObject(sheetpath)
Set xl = xlbook.Application

What's this?

xl.application.qit

It should be

xl.Quit

And clear your references in the correct order, from bottom to top:

set xlsheet = nothing
set xlbook = nothing
set xl = nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM