从Excel行值创建文本文件

时间:2019-06-10 06:06:30

标签: excel vba

我正在尝试编写在指定位置创建文本文件并且输入为cel值的代码

php -S localhost:8000

未显示错误。不明白为什么它不起作用

1 个答案:

答案 0 :(得分:1)

这将起作用:

Sub Create()

Dim myPathTo As String
myPathTo = "d:\users\"
Dim myFileSystemObject As Object
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim fileOut As Object
Dim myFileName As String

Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long

    For i = 2 To lastRow
        If Not IsEmpty(Cells(i, 1)) Then
            myFileName = Cells(i, 4) & ".txt"
            Set fileOut = myFileSystemObject.OpenTextFile(myPathTo & myFileName, 8, True)
            fileOut.Write Cells(i, 8)
            fileOut.Close
        End If
    Next

Set myFileSystemObject = Nothing
Set fileOut = Nothing


End Sub

您没有使用完整路径来保存文件。现在,您将在D:\Users\

中看到所有文本文件。