附加到分隔的文本文件

`我想在vba中使用一个macros:

  1. 在给定的Excel表格中选取所选单元格的值。 要求select是否没有选定的单元格。

  2. 检查是否存在某个文本文件(逗号分隔)。

  3. 如果文件存在,则附加值,创build文件并在文件不存在时添加值(逗号分隔)。

任何帮助将不胜感激。

Sub Wri() Dim myrng As Range Set myrng = Range("A1:A22222") Const ForReading = 1, ForWriting = 2, ForAppending = 3 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.OpenAsTextStream(ForWriting, TristateFalse) For Each cell In myrng cellv = cell.Value ts.Write (cellv & Chr(44)) Next cell End Sub 

`我想使用用户select和追加的范围

 Sub MainProc() Dim Rng As Range Dim Cell As Range On Error Resume Next Set Rng = Application.InputBox("Select range", Type:=8) On Error GoTo 0 If Rng Is Nothing Then MsgBox "No cells selected" Exit Sub End If For Each Cell In Rng.Cells WriteToFile Cell, "Hello World!" Next Cell End Sub Private Sub WriteToFile(Rng As Range, s As String) Dim FSO As FileSystemObject Dim TS As TextStream Set FSO = New FileSystemObject Set TS = FSO.OpenTextFile(Rng.Value, ForAppending, True, TristateFalse) TS.WriteLine s End Sub