我尝试打开类型为' .xlsm'的工作簿。使用
Private Function readFiles(ByVal lv_path As String, ByRef lx_wrkShDes As Worksheet)
'On Error GoTo ErrorHandling
Dim lx_objectExcel As New Excel.Application
Dim lx_wrkBkSrc As Workbook
Dim lx_wrkShSrc As Worksheet
Dim lx_shrPathObj As Object
Dim lv_shrPath As String
Set lx_shrPathObj = CreateObject("scripting.filesystemobject")
lv_shrPath = Replace(lx_shrPathObj.GetFile(lv_path).ShortPath, mv_longFilePathHelper, "")
Set lx_wrkBkSrc = Workbooks.Open(Filename:=lv_shrPath, ReadOnly:=True)
'Using lx_objectExcel.Workbooks.Open WORKS but not Workbooks.Open
'lx_objectExcel.Workbooks.Open(Filename:=lv_shrPath, ReadOnly:=True)
If Not Library.DoesSheetExist(lx_wrkBkSrc, mv_workSheetName) Then
GoTo ErrorHandling
End If
Set lx_wrkShSrc = lx_wrkBkSrc.Sheets(mv_workSheetName)
'Rest of the function
End Function
它会打开工作簿并立即退出VBA代码。 我尝试了这个https://support.microsoft.com/en-us/help/555263,但结果相同。
如果我使用
的新Excel实例,它不会退出lx_objectExcel.Workbook.open('path')
我不想使用新实例,因为粘贴特殊功能不适合新实例,并且以这种方式打开100多个工作簿会耗费大量时间。
答案 0 :(得分:3)
替换:
Workbook.open('path')
用
Workbooks.Open("PathName")
答案 1 :(得分:1)
参考Workbook.Open方法,您的操作的正确语法如下
Workbooks.Open("WorkbookPath")
以下代码完全适合我
Dim targetWorkbook As Workbook
Set targetWorkbook = Workbooks.Open("YourWorkbookPath")
您没有正确使用它。如果仍然无效,请确保为您的文件发送正确的路径。
答案 2 :(得分:0)
尝试使用2个xlsm文件(MASTER和SLAVE)的代码,它可以很好地形成我。 Office 2013
Dim sText As String
Dim objWB As Excel.Workbook
sText = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
Set objWB = Application.Workbooks.Open(ThisWorkbook.Path & "\SLAVE.XLSM")
objWB.Worksheets("Sheet1").Range("A1").Value = sText
MsgBox "DONE!"