首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用VBA/宏打开工作簿是否将其设置为只读?

使用VBA/宏打开工作簿是否将其设置为只读?
EN

Stack Overflow用户
提问于 2012-09-20 00:54:24
回答 1查看 6.6K关注 0票数 0

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

你能在我的开放代码中看到任何错误吗?我知道文件打开了,行搜索起作用了,但是它1.永远不会写入单元格,2.锁定文件。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-20 03:17:01

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

你就不能这样做吗:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12499359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档