使用 VBA/宏打开工作簿是否使其只读?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12499359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:46:38  来源:igfitidea点击:

Opening a workbook with VBA/macro is making it read only?

excelvbaexcel-vba

提问by okapishomapi

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.

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

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.

你能在我的打开代码中看到任何错误吗?我知道文件打开并且行搜索有效,但是它 1. 从不写入单元格,并且 2. 锁定文件。

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

Thank you again, stackoverflow <3

再次感谢,stackoverflow <3

回答by Stepan1010

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

您是否需要打开一个新的 Excel 应用程序才能打开工作簿?

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