【问题标题】:Export Excel workseet into txt file using vba - (text and numbers with formulas)使用 vba 将 Excel 工作表导出到 txt 文件 - (带有公式的文本和数字)
【发布时间】:2016-06-14 13:46:42
【问题描述】:

我对 vba 还很陌生,但我已经开发了一个代码,用于在 excel 中将文本/数字移动到 txt 文件。我的问题是我目前有三个函数需要转到同一个 txt 文件。当我当前运行它时,第一个函数有

Set fs = CreateObject("scripting.FileSystemObject")
Set a = fs.createTextFile(Module1.NYS45Uploadfilename, True)
a.writeline (str)
a.Close.

另外两个函数已经保存了。

Set fs = CreateObject("scripting.FileSystemObject")
Set a = fs.saveTextFile(Module1.NYS45Uploadfilename, True)
a.writeline (str)
a.Close.

第一个函数会去txt文件,另外两个我报错

对象不支持此属性或方法。

当我更改所有三个以创建最后一个函数时,转到 txt 文件。我不知道要使用什么词才能使其他两个函数按照键入的顺序进入 txt 文件。

整个代码如下

Private Sub addRecord_Click()

    On Error GoTo ErrHandler

    Module1.NYS45Uploadfilename = Application.GetSaveAsFilename(FileFilter:="Textfiles (*.txt), *.txt")

    If Module1.NYS45Uploadfilename = "False" Then Exit Sub

    Header_Rec

    Detail_Rec1

    Detail_Rec2

    Dim strmsg As String

    strmsg = "Your file has been added here: " & Module1.NYS45Uploadfilename

    MsgBox strmsg

    Exit Sub
ErrHandler:
    MsgBox Err.Number & vbNewLine & Err.Description
    Resume Next

End Sub


Function Header_Rec()

    Dim str, strfilename, txtpath As String

    Dim strlencount, strspacer As Integer

    str = str & Range("a3").Value
    str = str & Range("b3").Value
    str = str & Trim(Range("c3").Value)
    strlencount = Len(Trim(Range("c3").Value))
    strspacer = 30 - strlencount
    str = Module1.SpaceAdd(str, strspacer)
    str = str & Range("d3").Value
    str = str & Range("E3").Value

    str = Module1.SpaceAdd(str, 159)

    ' Debug.Print Len(str)

    Set fs = CreateObject("scripting.FileSystemObject")
    Set a = fs.createTextFile(Module1.NYS45Uploadfilename, True)
    a.writeline (str)
    a.Close
    ' Debug.Print str

End Function

Function Detail_Rec1()

    Dim str, strnum, str2, strfilename, txtpath As String

    Dim strlencount, strspacer As Integer

    If Range("a7").Value <> "5" Then
        Exit Function
    End If

    str = str & Range("a7").Value
    str = str & Range("b7").Value
    str = str & Range("c7").Value

    str = Module1.SpaceAdd(str, 1)
    str = str & Trim(Range("d7").Value)
    strlencount = Len(Trim(Range("d7").Value))
    strspacer = 30 - strlencount
    str = Module1.SpaceAdd(str, strspacer)
    str = str & Range("E7").Value

    ' Debug.Print Len(str)

    Set fs = CreateObject("scripting.FileSystemObject")
    Set a = fs.addTextFile(Module1.NYS45Uploadfilename, True)
    a.writeline (str)
    a.Close
    ' Debug.Print str

End Function

Function Detail_Rec2()

    Dim str, strnum, str2, strfilename, strnew, txtpath As String

    Dim strlencount, strspacer As Integer

    If Range("a11").Value <> "6" Then
        Exit Function
    End If

    str = str & Range("a11").Value
    str = str & Range("b11").Value
    str = str & Trim(Range("c11").Value)
    strlencount = Len(Trim(Range("c11").Value))
    strspacer = 11 - strlencount
    str = Module1.SpaceAdd(str, strspacer)
    strspacer = 30 - strlencount
    str = Module1.SpaceAdd(str, strspacer)
    str = str & Range("f11").Value
    str = str & Range("g11").Value

    ' Debug.Print Len(str)

    Set fs = CreateObject("scripting.FileSystemObject")
    Set a = fs.getTextFile(Module1.NYS45Uploadfilename, True)
    a.writeline (str)
    a.Close
    ' Debug.Print str

End Function

【问题讨论】:

  • 请编辑您的问题,分享您在 excel 中使用的代码以将文本/数字移动到 txt 文件,修复格式 - 使用代码块而不是内联代码。
  • 我很抱歉你要什么?你想要到目前为止我为整个工作表制作的整个代码吗?我可以把它复制粘贴到这里吗
  • FileSystemObject 对象没有这样的 saveTextFile 方法。我知道“第一个函数”的目标是创建一个 TextStream 对象并写入它。但是“其他两个功能”的目标是什么?
  • 每个函数都有一行要在 txt 文件中创建。此 txt 文件用于测试和上传到程序以获得所需的结果。我将包含整个代码供您查看。
  • 但我一直看到 "strange" 方法,例如 getTextFileaddTextFile、... 你在用 VBA 编码吗?

标签: excel text-files vba


【解决方案1】:

考虑以下示例,该示例显示如何使用 OpenTextFile method 将文本写入文件:

Sub Test()

    Const ForWriting = 2
    Const ForAppending = 8
    Const FormatDefault = -2
    Const FormatUnicode = -1
    Const FormatASCII = 0

    strFile = "C:\TestFile.txt"

    strText = "New Unicode text file created, this is the first line." & vbCrLf
    WriteTextFile strFile, strText, ForWriting, FormatUnicode

    strText = "New line added, this is the second line." & vbCrLf
    WriteTextFile strFile, strText, ForAppending, FormatUnicode

    strText = "One more line." & vbCrLf
    WriteTextFile strFile, strText, ForAppending, FormatUnicode

End Sub

Sub WriteTextFile(strPath, strContent, lngMode, lngFormat)
    ' strPath: path to the text file
    ' strContent: text to be written to the file
    ' lngMode: 2 - For Writing, 8 - For Appending
    ' lngFormat: -2 - System default, -1 - Unicode, 0 - ASCII
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, lngMode, True, lngFormat)
        .Write strContent
        .Close
    End With
End Sub

【讨论】:

  • 我真的想通了。我的问题是我要求它继续访问文件,而我应该在最后使用函数导出到文本来询问。这解决了。现在进入我的下一个问题。感谢您抽出宝贵时间查看此内容。
  • @LizaHunt,如果您的问题已得到解决,请分享工作代码作为答案,或单击接受此答案,这可能对将来的某人有所帮助。
  • 我的问题实际上并没有得到回答,因为我试图做的事情没有奏效。我所做的只是没有创建对象;最后我使用 ExportToText 作为函数,并在每个函数中使用硬返回编码在记事本中创建测试文件。我仍然无法将其转到我想要的目录,但这是更多的研究,希望我能弄清楚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 2013-11-11
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
相关资源
最近更新 更多