【问题标题】:Append to Text File VBA附加到文本文件 VBA
【发布时间】:2014-03-15 19:29:51
【问题描述】:

我需要将选定范围内的值放入以逗号分隔的文本文件并附加它们。下面的代码在 Set TS 处给了我一个错误。为什么??

Sub Wri()

Dim myrng As Range
Dim Cell As Range

On Error Resume Next
Set myrng = Application.InputBox("Select range", Type:=8)
On Error GoTo 0

If myrng Is Nothing Then
    MsgBox "No cells selected"
    Exit Sub
End If

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim fs, f, TS, s
Dim cellv As String

Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile "C:\Users\HP\Documents\fil.txt"
Set f = fs.GetFile("C:\Users\HP\Documents\fil.txt")
Set TS = f.OpenTextFile(myrng.Value, 8, True, 0)

For Each Cell In myrng
    cellv = Cell.Value
    TS.Write (cellv & Chr(44))
Next Cell

End Sub

【问题讨论】:

    标签: vba excel csv


    【解决方案1】:

    我给你做了一个自定义子,用这两个替换子 - 最后一个参数决定它是否是附加的,它也会处理新行:D

    Sub writeCSV(ByVal thisRange As Range, ByVal filePath As String, Optional ByVal fileAppend As Boolean = False)
        Dim cLoop As Long, rLoop As Long
        Dim ff As Long, strRow As String
    
        ff = FreeFile
        If fileAppend Then
            Open filePath For Append As #ff
        Else
            Open filePath For Output As #ff
        End If
    
        For rLoop = 1 To thisRange.Rows.Count
            strRow = ""
            For cLoop = 1 To thisRange.Columns.Count
                If cLoop > 1 Then strRow = strRow & ","
                strRow = strRow & thisRange.Cells(rLoop, cLoop).Value
            Next 'cLoop
            Print #ff, strRow
        Next 'rLoop
    
        Close #ff
    End Sub
    
    Sub Wri()
    
    Dim myrng As Range
    Dim Cell As Range
    
    On Error Resume Next
    Set myrng = Application.InputBox("Select range", Type:=8)
    On Error GoTo 0
    
    If myrng Is Nothing Then
        MsgBox "No cells selected"
        Exit Sub
    Else
        writeCSV myrng, "C:\Users\HP\Documents\fil.txt", True
    End If
    
    End Sub
    

    【讨论】:

    • 这行得通!但是有一个小问题,如果我垂直选择一个范围,它会垂直添加值,如果我选择一个水平范围,它会水平添加值。无论哪种情况,输出都可以限制为水平放置吗??
    • 抱歉,我无法复制。你能给我一个例子“垂直范围”
    • 如果我通过垂直拖动来选择单元格,比如从 A1 到 A7,值将垂直放置在文本文件中(占用 7 行),如果我通过从 A1 到 E1 拖动来选择单元格,它们水平显示(占 1 行)
    • 啊,对了,现在我和你在一起——你只想要一行或一列?
    • 我希望输出仅在行中,而不管范围是如何选择的。每次宏运行时,一行后有一个换行符。
    【解决方案2】:

    试试

    Sub Wri()
    
    Dim myrng As Range
    Dim Cell As Range
    
    On Error Resume Next
    Set myrng = Application.InputBox("Select range", Type:=8)
    On Error GoTo 0
    
    If myrng Is Nothing Then
        MsgBox "No cells selected"
        Exit Sub
    End If
    
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    
    Dim fs, f, TS, s
    Dim cellv As String
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CreateTextFile "C:\Users\HP\Documents\fil.txt"
    Set TS = fs.OpenTextFile("C:\Users\HP\Documents\fil.txt", 8, True, 0)
    
    For Each Cell In myrng
        cellv = Cell.Value
        TS.Write (cellv & Chr(44))
    Next Cell
    
    End Sub
    

    【讨论】:

    • 设置 TS=f..,“运行时错误 438,对象不支持此属性或方法”设置 TS=fs..,“运行时错误 13,类型不匹配”
    • 我认为您需要删除“Set f = fs.GetFile("C:\Users\HP\Documents\fil.txt")”并更改 Set TS=f。设置 TS=fs。
    • 完成,Set TS = fs.OpenTextFile(myrng.Value, 8, True, 0) 有错误提示类型不匹配
    • 糟糕,myrng.Value 应该是什么? msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
    • 工作!!!谢谢!但是,如果我运行宏两次,它会覆盖值而不是追加,如何解决?
    【解决方案3】:

    将所有数据添加到“列表”

    Sub writeList(ByVal thisRange As Range, ByVal filePath As String, Optional ByVal fileAppend As Boolean = False)
        Dim cLoop As Long, rLoop As Long
        Dim ff As Long, strRow As String
        Dim tCell As Range
        ff = FreeFile
        If fileAppend Then
            Open filePath For Append As #ff
        Else
            Open filePath For Output As #ff
        End If
        For Each tCell In thisRange
            Print #1, tCell.Value
        Next tCell
        Close #ff
    End Sub
    
    Sub Wri()
    
    Dim myrng As Range
    Dim Cell As Range
    
    On Error Resume Next
    Set myrng = Application.InputBox("Select range", Type:=8)
    On Error GoTo 0
    
    If myrng Is Nothing Then
        MsgBox "No cells selected"
        Exit Sub
    Else
        writeList myrng, "C:\Users\HP\Documents\fil.txt", True
    End If
    
    End Sub
    

    【讨论】:

    • 垂直显示,需要水平显示!你摆脱了逗号分隔符,需要那些:p
    • 对不起,试试我添加的 writeHList,应该可以。
    【解决方案4】:

    嗯,尝试将调用 writeList 更改为 writeHList 然后使用这个子:

    Sub writeHList(ByVal thisRange As Range, ByVal filePath As String, Optional ByVal fileAppend As Boolean = False)
        Dim cLoop As Long, rLoop As Long
        Dim ff As Long, strRow As String
        Dim tCell As Range
        Dim strLine
        ff = FreeFile
        If fileAppend Then
            Open filePath For Append As #ff
        Else
            Open filePath For Output As #ff
        End If
    
        For Each tCell In thisRange
            If strLine = "" Then
                strLine = tCell.Value
            Else
                strLine = strLine & "," & tCell.Value
            End If
        Next tCell
        Print #1, tCell.Value
        Close #ff
    End Sub
    

    【讨论】:

    • 试过了,仍然给出一个没有逗号分隔符的垂直列表。我可以使用第一个自定义子,但是问题几乎解决了:)
    • 奇怪,对我有用......虽然它确实在之后添加了 CR。需要将行 writeList myrng, "C:\Users\HP\Documents\fil.txt", True 更改为 writeHList myrng, "C:\Users\HP\Documents\fil.txt", True 以使用新的子
    • 有一个新问题,如果我使用合并单元格(多行合并为一个),它会给我剩余行的空白逗号分隔值。我该如何避免这种情况??
    • 哦,不确定 - 这可能需要一些开发工作(如果合并单元格范围的 >index0 则忽略)。我现在太忙于其他工作了,可能值得有人解决一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多