[英]Loop over folder of workbooks and export all sheets to tab-delimited text with Excel VBA
I pieced together an Excel VBA script that writes all worksheets in an open workbook to separate, tab-delimited files (is this still a "macro"? I'm learning this in an Excel vacuum). 我拼凑了一个Excel VBA脚本,该脚本将所有工作表写在一个打开的工作簿中,以分隔制表符分隔的文件(这仍然是一个“宏”吗?我正在Excel吸尘器中学习它)。 It works well on one workbook at a time.
一次可以在一个工作簿上很好地工作。 Here it is.
这里是。
Sub exportSheetsToText()
Dim sWb As String
Dim sFile As String
Dim oSheet As Worksheet
sWb = Left(ActiveWorkbook.FullName, InStr(ActiveWorkbook.FullName, ".") - 1)
For Each oSheet In Worksheets
oSheet.Copy
sFile = sWb & "-" & oSheet.Name & ".txt"
ActiveWorkbook.SaveAs fileName:=sFile, FileFormat:=xlText
ActiveWorkbook.Close SaveChanges:=False
Next oSheet
End Sub
I would like to scale this up so that I can apply this macro to a folder of workbooks. 我想扩大规模,以便可以将此宏应用于工作簿文件夹。 I wrote what I thought would loop over every workbook that satisfies the filter, but it doesn't write any of the .txt files.
我写了我认为会遍历所有满足筛选条件的工作簿的内容,但是它没有写任何.txt文件。 Here it is.
这里是。
Sub exportsSheetsToTextForAll()
Dim sPath As String
Dim sWildcard As String
Dim sMacro As String
Dim oWb As Workbook
Dim oPersWb As Workbook
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Set oPersWb = Workbooks("PERSONAL.XLSB")
sMacro = "'" & oPersWb.Name & "'" & "!exportSheetsToText()"
sPath = "C:\Users\richard\Documents\Research\Data\Excel\Datastream - payout"
sWildcard = "New*.xlsx"
sFile = Dir(sPath & "\" & sWildcard)
Do While Len(sFile) > 0
Workbooks.Open Filename:=sPath & "\" & sFile
Application.Run sMacro
ActiveWorkbook.Close SaveChanges:=False
sFile = Dir
Loop
End Sub
It loops through all of my test files, but I don't see any effects (ie, no .txt files and no errors). 它循环遍历我所有的测试文件,但是我看不到任何影响(即没有.txt文件且没有错误)。
Eventually I will run this on very large workbooks with macros, so it is important to disable the macros (I don't have the macros locally, they're on a dedicated data machine) and close one large workbook before opening the next. 最终,我将在带有宏的非常大的工作簿上运行此命令,因此禁用宏(我本地没有宏,它们在专用数据机上)并关闭一个大工作簿,然后再打开下一个工作簿,这一点很重要。
Any ideas? 有任何想法吗? Thanks!
谢谢!
@Siddarth's idea of passing an argument to exportSheetsToText()
was the key. exportSheetsToText()
将参数传递给exportSheetsToText()
的想法是关键。 As well I had an error with macro name passed to Application.Run
. 同样,我在将宏名称传递给
Application.Run
出错。 The following works and is much cleaner. 以下工作原理更清洁。
Sub exportsSheetsToTextForAll()
Application.AutomationSecurity = msoAutomationSecurityForceDisable
excelFiles = Dir(ThisWorkbook.Path & "\" & "New*.xlsx")
fromPath = ThisWorkbook.Path
Do While Len(excelFiles) > 0
Debug.Print Files
Set oWb = Workbooks.Open(Filename:=fromPath & "\" & excelFiles)
Application.Run "exportSheetsToText", oWb
oWb.Close SaveChanges:=False
excelFiles = Dir
Loop
End Sub
Sub exportSheetsToText(iWb As Workbook)
For Each ws In iWb.Worksheets
ws.Copy
Set wb = ActiveWorkbook
textFile = Left(iWb.FullName, InStr(iWb.FullName, ".") - 1) & "-" & ws.Name & ".txt"
wb.SaveAs Filename:=textFile, FileFormat:=xlText
wb.Close SaveChanges:=False
Next ws
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.