我有下面的代码打开一个文件夹中的每个.xlsx文件,并对其调用另一个子文件。当它到达打开的文件时(由于前面的子项而不可避免地将其打开),我需要它来跳过此文件并继续。这是从在线来源修改的,因为我不完全理解使用路径。预先感谢!
Sub OpenFilesVBA()
Dim Wb As Workbook
Dim strFolder As String
Dim strFil As String
Application.ScreenUpdating = False
strFolder = Application.ActiveWorkbook.path
strFil = Dir(strFolder & "\*.xlsx*")
Do While strFil <> vbNullString
Set Wb = Workbooks.Open(strFolder & "\" & strFil)
'===========Run Objective Macro==========================================
Call PoG_Report_Prep
'========================================================================
ActiveWorkbook.Save
Wb.Close False
strFil = Dir
Loop
End Sub
答案 0 :(得分:1)
大概包含此代码的文件另存为.xlsm(而不是.xlsx),这意味着该文件将被跳过,因为代码仅查找另存为.xlsx的文件
如果文件另存为.xlsx,则可以检查打开的文件是否与当前文件的文件名匹配,仅在名称不同时才运行代码。请参见下面的示例代码。 MsgBox语句仅用于测试目的,请在最终代码中将其删除。
Sub OpenFilesVBA()
Dim Wb As Workbook
Dim strFolder As String
Dim strFil As String
Dim strActiveFil As String '
Application.ScreenUpdating = False
strFolder = Application.ActiveWorkbook.Path
strActiveFil = Application.ActiveWorkbook.Name
strFil = Dir(strFolder & "\*.xlsx*")
Do While strFil <> vbNullString
MsgBox strFil
If strFil <> strActiveFil Then
MsgBox "run" '
Set Wb = Workbooks.Open(strFolder & "\" & strFil)
'===========Run Objective Macro==========================================
'Call PoG_Report_Prep
'========================================================================
ActiveWorkbook.Save
Wb.Close False
strFil = Dir
Else
MsgBox "not run"
strFil = Dir
End If
Loop
End Sub
答案 1 :(得分:1)
我最终以新的(非.xlsx)格式进行了保存,并杀死了原始文件。我使用.xlsm保留原始格式,但是.csv也可以。这是从下载的报告中获得的,因此,如果我需要获得被杀死的原件的新副本并不难。我整个模块的第一步如下:
'OF = Original File
Dim strFullName As String, OF As String, CurrentWB As Workbook
Set CurrentWB = ActiveWorkbook
strFullName = CurrentWB.path & "\" & Left(CurrentWB.Name, Len(CurrentWB.Name) - 5) &
".xlsm"
OF = Application.ActiveWorkbook.FullName
ActiveWorkbook.SaveAs Filename:=strFullName,
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'' Delete Original File
Kill OF
答案 2 :(得分:0)
这应该有效。只要包含此代码的文件另存为* .xlsm
total = []
for i in [a, b, c,...]:
if isinstance(i, list):
total += i
else:
total.append(i)
答案 3 :(得分:0)
首先检查工作簿的状态:
Set Wb = Workbooks.Open(strFolder & "\" & strFil)
If Not Wb.ReadOnly Then '// workbook is readonly
PoG_Report_Prep
Wb.Save
End If
Wb.Close
Set Wb = Nothing
如果打开后的ReadOnly
状态为True
,那么我们可以假定在用代码打开工作簿之前,该工作簿已在其他位置打开。
有点脏,但是可以满足您的目的。