我是Access和VBA的新手,并尝试开发一个简单的代码:将表导出到xls,打开它,简单操作(格式化),保存和关闭。
但是我在此过程中收到以下消息框:"A file named RESUME.XLW" already exists in this location. Do you want to replace it?"
选择“是”将生成xls文件。但是当我尝试打开它时,Excel以只读模式运行,我不明白为什么。
我正在使用以下代码:
Sub FormataExcelPadrao(caminhoExcel As String)
Set arquivoExcel = CreateObject("Excel.Application")
arquivoExcel.Workbooks.Open (caminhoExcel)
With arquivoExcel
For Each pagina In .Worksheets
With pagina
.Columns("A:Z").Autofit
.Cells.Font.Size = "10"
.Cells.Font.Name = "Calibri"
End With
Next pagina
End With
arquivoExcel.Save
arquivoExcel.Close
End Sub
提前致谢!
答案 0 :(得分:6)
定义对象然后使用它。见这个例子
Sub FormataExcelPadrao(caminhoExcel As String)
Dim arquivoExcel As Object, wb As Object, pagina As Object
Set arquivoExcel = CreateObject("Excel.Application")
Set wb = arquivoExcel.Workbooks.Open(caminhoExcel)
With wb '<~~ Also this has to be the workbook and not excel app
For Each pagina In .Worksheets
With pagina
.Columns("A:Z").AutoFit
.Cells.Font.Size = "10"
.Cells.Font.Name = "Calibri"
End With
Next pagina
End With
wb.Close SaveChanges:=True
arquivoExcel.Quit
Set wb = Nothing
Set arquivoExcel = Nothing
End Sub
答案 1 :(得分:0)
替换它:
arquivoExcel.Save
使用:
arquivoExcel.ActiveWorkbook.Save