VBA宏在打开某些工作簿时停止运行。当我在中断模式下运行代码时,似乎控制转到新打开的工作簿,并且没有任何指示可以遵循。
我正在尝试打开很多工作簿,打印输出并关闭它们。
我也试过在只读模式下打开它们,将计算模式设置为手动但到目前为止没有任何工作。
答案 0 :(得分:0)
如果您有一个包含所有工作簿的文件夹,则以下内容将起作用。 此代码将遍历给定文件夹中的一系列excel文件:
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strPath As String
Dim intLen As Integer
Dim strName As String
Dim wsSheet As Worksheet
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\Users\username\Desktop\FolderName" 'Set path of folder with excel files
Set objFolder = objFSO.GetFolder(strPath) 'Get the folder object associated with the directory
For Each objFile In objFolder.Files 'Loop through the files in the folder
Workbooks.Open (strPath & "\" & objFile.Name)
Set wsSheet = Worksheets("Sheet1") 'Name of sheet in excel file
ActiveWorkbook.CheckCompatibility = False 'Skip compatibility check
此代码会将每张纸上指定范围的pdf打印到指定路径:
intLen = Len(objFile.Name) - 5 'Remove .xlsx characters from name
strName = Left(objFile.Name, intLen)
wsSheet.Range("A1:P58").ExportAsFixedFormat xlTypePDF, "C:\Users\username\Desktop\" & strName & ".pdf", , , , , , False
此代码将关闭每个文件而不保存,并继续循环浏览文件夹:
Workbooks(strName).Close SaveChanges:=False 'Close excel file without saving
Next