Outputting .csv rows to a series of text files

时间:2015-07-08 15:41:28

标签: excel csv text-files

How to output .csv rows to a series of seperate text files?

I saw this link, and the approach should be similar.. Outputting Excel rows to a series of text files

Can anyone help me know the steps and if i should use excel and suggest the steps, to achieve the above results as in the link or otherwise. Thanks in advance...

1 个答案:

答案 0 :(得分:0)

假设您现在正在使用Excel ...

首先,就像我在其中一条评论中所说,你应该开始here来了解如何自己使用Visual Basic for Applications(VBA)。

在Excel中,最简单的方法是激活 Developer 选项卡以访问Visual Basic编辑器。

  1. 转到文件> 选项> 自定义功能区
  2. 检查开发人员标签。单击“确定”。
  3. 转到开发人员Code部分下是 Visual Basic 。单击此按钮打开VBA编辑器。
  4. 现在你可以制作VBA潜艇了。您可以将它们放在特定工作表,整个工作簿或PERSONAL.XLSB中的模块中,这些模块允许任何启用了宏的Excel工作簿来运行它们。

    您可能需要创建一个模块。如果是这样的话:

    1. 右键点击 VBAProject (PERSONAL.XLSB)
    2. Insert> Module
    3. 命名。
    4. 您现在可以将代码粘贴到模块中,并且有一个"宏"。

      最简单的方法是

      1. 转到开发人员标签。
      2. 点击
      3. 选择您的宏。
      4. 点击运行
      5. 您还可以通过在同一Macros菜单中点击选项... 来指定键盘快捷键。

        现在,您可以从here复制并粘贴代码。稍作修改:

        Sub Export_Files()
            Dim sExportFolder, sFN
            Dim rTitle As Range
            Dim rContent 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 rTitle In oSh.UsedRange.Columns("A").Cells
                Set rContent = rTitle.Offset(, 1) & ", " & rTitle.Offset(, 2)   '<--This will put in your Column B and C value. You can delimit with whatever you desire; I used a comma and space.
                'Add .txt to the article name as a file name
                sFN = rTitle.Value & ".txt"
                Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
                oTxt.Write rContent.Value
                oTxt.Close
            Next
        End Sub