用VBA/宏打开工作簿是只读的吗? [英] Opening a workbook with VBA/macro is making it read only?

查看:92
本文介绍了用VBA/宏打开工作簿是只读的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的代码打开一个工作簿(总是相同的),检测第一个空闲行,只写该行中的两个单元格,然后保存/关闭工作簿.这似乎是一个简单的问题,但是宏似乎正在打开文件的副本,然后将其锁定以进行编辑.

I would like my code to open a workbook (always the same one), detect the first free row, write to just two cells in that row, and then save/close the workbook. This seems like a simple problem, but the macro seems to be opening a copy of the file, and then locking it for editing.

您可以在我的开放代码中看到任何错误吗?我知道该文件已打开并且行搜索有效,但随后它1.永远不会写入单元格,并且2.锁定该文件.

Can you see any errors in my open code? I know that the file opens and that the row search works, but then it 1. never writes to the cells, and 2. locks the file.

Function WriteToMaster(Num, Path) As Boolean

'Declare variables
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
Dim infoLoc As Long

Set xlApp = New Excel.Application

'Specifies where the Master Move Key is stored
Set wb = xlApp.Workbooks.Open("DOC LOCATION")
Set ws = wb.Worksheets("Sheet1")

'Loop through cells, looking for an empty one, and set that to the loan number
infoLoc = firstBlankRow(ws)
MsgBox "First blank row is " & infoLoc & ". Num is " & Num

ws.Cells(infoLoc, 1).Value = Num
ws.Cells(infoLoc, 2).Value = Path


'Save, close, and quit
wb.Save
wb.Close
xlApp.Quit

'Resets the variables
Set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing

'pieces of function from http://p2p.wrox.com/vb-how/30-read-write-excel-file-using-vb6.html
End Function

再次感谢您,stackoverflow< 3

Thank you again, stackoverflow <3

推荐答案

是否只需要打开工作簿就需要打开一个新的excel应用程序?

Do you need to open a new excel app just to open a workbook?

你不能只做这样的事情吗?

Can't you just do something like this:

Sub Macro1()

Dim wkb As Workbook
Workbooks.Open Filename:="\User Documents$\bob\My Documents\workbook_open_example.xlsx"
Set wkb = Workbooks("workbook_open_example.xlsx")

End Sub

这篇关于用VBA/宏打开工作簿是只读的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆