适用于Mac OSX的Excel VBA代码:将Excel行输出到一系列文本文件(用于主题建模)

时间:2017-06-10 05:52:30

标签: excel vba macos topic-modeling excel-vba-mac

我正在进行第一次主题建模,我正处于使用宏生成电子表格中每一行的单独文本文件的阶段(如下所述:keyboardShouldPersistTaps)。该页面上提到的代码并不是我需要做的,所以我使用的是这段代码:http://www.themacroscope.org/?page_id=418

应该为每一行创建一个新的文本文件,使用列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上运行?我正在使用Excel 2011和VBA 14.0,如果这有所不同。

1 个答案:

答案 0 :(得分:0)

如果这是一台Windows PC,以下内容可以让您避免使用FileSystemObject

Sub Export_Files()
    Dim sExportFolder As String
    Dim rArticleName As Range
    Dim rDisclaimer As Range
    Dim oSh As Worksheet

    'sExportFolder = path to the folder you want to export to
    'oSh = The sheet where your data is stored
    sExportFolder = "C:\Disclaimers"
    Set oSh = Sheet1

    For Each rArticleName In oSh.UsedRange.Columns("A").Cells
        Set rDisclaimer = rArticleName.Offset(, 1)
        Open sExportFolder & Application.PathSeparator & rArticleName.Value & ".txt" For Output As #1
        Print #1, rDisclaimer.Value
        Close #1    
    Next
End Sub

我不知道这是否适用于Mac。