跳过打开的文件

问题描述 投票:0回答:4

我有下面的代码打开每个.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
excel vba
4个回答
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

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

0
投票

这应该工作。只要包含此代码的文件保存为* .XLSM

Option Explicit

Sub OpenFilesVBA()
    Dim Wb As Workbook
    Dim strFolder As String
    Dim strFil As String
    Dim fso As Object
    Dim fil As Object
    Dim fldr As Object

    Application.ScreenUpdating = False

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set fldr = fso.GetFolder(Application.ActiveWorkbook.Path)

    For Each fil In fldr.Files
        If LCase(fso.GetExtensionName(fil.Path)) = "xlsx" Then
            Set Wb = Workbooks.Open(fil.Path)
'===========Run Objective Macro==========================================
            Call PoG_Report_Prep
'========================================================================
            ActiveWorkbook.Save
            Wb.Close False
            Set Wb = Nothing
        End If
    Next

    Set fil = Nothing
    Set fldr = Nothing
    Set fso = Nothing

    Application.ScreenUpdating = True

End Sub

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那么我们或许可以假设该工作簿已经打开的其他地方,你在代码中打开它之前。

这是一个有点脏,但将努力为你的目的。

© www.soinside.com 2019 - 2024. All rights reserved.