Vba 打开文件夹中的多个文件应用宏并将它们保存到具有不同名称(如1、2、3)的不同文件夹中
我试图在VBA上编写代码,允许我访问预定文件夹1中的所有文件。打开每个文件,应用一个感兴趣的宏,然后将最终结果复制到预定文件夹中的不同工作簿中。2将它们保存为.csv文件 我的代码的问题是,当我保存添加到预定文件夹2中的工作簿时出现问题。我总是用相同的名称保存它,这会造成重叠 另一个问题是,当我尝试关闭wb.close(请参阅下面的代码)时,我会收到savechanges y/n提示。 我已经写了一段代码,失败得很惨。我需要专家的帮助。再次感谢你们的支持 最好的 拉米Vba 打开文件夹中的多个文件应用宏并将它们保存到具有不同名称(如1、2、3)的不同文件夹中,vba,excel,Vba,Excel,我试图在VBA上编写代码,允许我访问预定文件夹1中的所有文件。打开每个文件,应用一个感兴趣的宏,然后将最终结果复制到预定文件夹中的不同工作簿中。2将它们保存为.csv文件 我的代码的问题是,当我保存添加到预定文件夹2中的工作簿时出现问题。我总是用相同的名称保存它,这会造成重叠 另一个问题是,当我尝试关闭wb.close(请参阅下面的代码)时,我会收到savechanges y/n提示。 我已经写了一段代码,失败得很惨。我需要专家的帮助。再次感谢你们的支持 最好的 拉米 我想我在某种程度上做了正
我想我在某种程度上做了正确的事情,这是我的新代码。但是,我仍然无法告诉excel在我编写wb.close时不要问我是否要保存文件 无论如何,这是我的密码
Sub Ram2()
'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
Dim i As Integer
i = 1
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\" & CStr(i) & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
i = i + 1
'Close the file (it was saved in Macro6 already)
wb.Close SaveChanges:=False
Next
Set file = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub
将Application.DisplayAlerts=False放在子文件夹的顶部附近。将Application.DisplayAlerts=True放在子文件夹的末尾。替换
wb。用wb关闭。关闭保存更改:=True
在关闭工作簿之前保存更改。我不想保存更改,我只想不保存而退出,不保存每次告诉excel我不想保存Rami时,如果此代码不能解决您的问题,您不应该将其放入“答案”中。单击问题下方的“编辑”链接,将代码添加到问题本身,并给出解释。如果此代码确实解决了您的问题,请单击此答案下方的“编辑”,并添加对其帮助方式的说明。
Sub Ram2()
'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
Dim i As Integer
i = 1
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\" & CStr(i) & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
i = i + 1
'Close the file (it was saved in Macro6 already)
wb.Close SaveChanges:=False
Next
Set file = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub