【发布时间】:2017-06-10 05:52:29
【问题描述】:
我正在做我的第一个主题建模,我正处于使用宏从电子表格的每一行中生成单独的文本文件的阶段(如此处所述:http://www.themacroscope.org/?page_id=418)。该页面上提到的代码并不是我需要做的,所以我使用这个代码:Outputting Excel rows to a series of text files
应该为每一行创建一个新的文本文件,使用 A 列作为文件名,使用 B 列作为内容。
Sub Export_Files()
Dim sExportFolder, sFN
Dim rArticleName As Range
Dim rDisclaimer As Range
Dim oSh As Worksheet
Dim oFS As Object
Dim oTxt As Object
'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "C:\Disclaimers"
Set oSh = Sheet1
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rArticleName In oSh.UsedRange.Columns("A").Cells
Set rDisclaimer = rArticleName.Offset(, 1)
'Add .txt to the article name as a file name
sFN = rArticleName.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
oTxt.Write rDisclaimer.Value
oTxt.Close
Next
End Sub
但是,它给了我这个错误信息:
Run-time error '429':
ActiveX component can't create object.
原来是因为我使用的是 Mac。当我点击 Debug 按钮时,它以黄色突出显示这一行:
Set oFS = CreateObject("Scripting.Filesystemobject")
有没有办法调整这条线,让它在 Mac 上工作?我正在使用带有 VBA 14.0 的 Excel 2011,如果这会有所不同的话。
【问题讨论】:
-
如果这是 Windows,我会通过使用
Open sExportFolder & Application.PathSeparator & rArticleName.Value & ".txt" For Output As #1Print #1, rDisclaimer.ValueClose #1来避免使用FileSystemObject,但我不知道这是否适用于 Mac。 -
我用该行替换了“Set oFS = CreateObject("Scripting.Filesystemobject")”。它给了我这个错误:编译错误:语法错误。我该怎么办?有没有办法在这些 cmets 上获得回车?
-
我会将建议作为“答案”发布,以便您查看换行符的位置,并将其放在正确的位置。
标签: excel vba macos topic-modeling