打开文件夹中的许多文件应用宏并将其保存到具有不同名称的不同文件夹,如1,2,3

时间:2016-02-23 14:57:46

标签: vba excel-vba excel

我正在尝试在VBA上编写代码,允许我访问预定文件夹中的所有文件1.打开每个文件,应用感兴趣的宏,然后将最终结果复制到预定文件夹中的不同工作簿2保存那里有.csv文件

我的代码的问题是,当我保存添加到预定文件夹2的工作簿时,这是一个问题。我总是使用相同的名称保存它,这会产生重叠。

另一个问题是当我尝试关闭wb.close时(请参阅下面的代码)我得到了保存更改y / n提示符。 enter image description here

我已经编写了一个代码并且失败了。我需要专家帮助。再次感谢您的支持。

最佳

拉米

Sub LoopFile()
    'Enable reference to Microsoft Scripting Runtime if you want to use early binding
    Dim fso As Object  'Scritping.FileSystemObject
    Dim fldr As Object 'Scripting.Folder
    Dim file As Object 'Scripting.File
    Dim wb As Workbook

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("D:\Rami Chehab\University Degrees & Courses\PhD in Labour Economics\Data\Data 2016\UNCTAD\1\nOT DONE COUNTRIES\")

    For Each file In fldr.Files
        'Open the file
        Set wb = Workbooks.Open(file.Path)
        '## You will need to modify this line to refer to the correct
        '    module name and macro name:
        Application.Run "PERSONAL.XLSB!Ramroum"
        Cells.Select
        Range("F7").Activate
        Application.CutCopyMode = False
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Paste
        Application.CutCopyMode = False
        ' The problem here it is only saving the folder as name 1 and not changing for example for 1 then in the other loop to 2, 3 and so on and so forth
        ' I think I need your help here in my code
        ChDir _
            "D:\Rami Chehab\University Degrees & Courses\PhD in Labour Economics\Data\Data 2016\UNCTAD\Okay"
        ActiveWorkbook.SaveAs Filename:= _
            "D:\Rami Chehab\University Degrees & Courses\PhD in Labour Economics\Data\Data 2016\UNCTAD\Okay\1.xlsx" _
            , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWindow.Close
        'Close the file (it was saved in Macro6 already)
        wb.Close
    Next

    Set file = Nothing
    Set fldr = Nothing
    Set fso = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

我认为我在某种程度上做了一些正确的事情,这是我的新代码。但是,当我写wb.close时,我仍然无法告诉excel不要问我是否要保存文件

无论如何这里是我的代码

this_thread_interrupt_flag

答案 1 :(得分:0)

在Sub的顶部附近放置Application.DisplayAlerts = False。在您的子结尾处放置了Application.DisplayAlerts = True。

相关问题