尝试在VBA中存储文本文件行

时间:2011-04-26 23:54:34

标签: excel-vba vba excel

问候,我希望能帮助找出如何将读取的文本文件的每一行存储为VBA程序作为字符串。我想修改其中一个字符串,然后将它们全部重新组合在一起,但不知道如何读取文本文件并以智能方式将每一行存储为单独的变量。感谢您的任何帮助,您可以提供!

2 个答案:

答案 0 :(得分:1)

查看FileSystemObject(参考:123

您必须转到菜单并添加Microsoft Scripting Runtime并创建全局变量Global fso as New FileSystemObject。现在,代码中的任何位置都会执行fso.OpenTextFile()之类的操作,返回TextStream。每个TextStream都有方法ReadLine()ReadAll()SkipLine()WriteLine()等等...

VBA References

以下是一个快速示例代码。

Sub TEST()
    Dim ts As TextStream
    Set ts = fso.OpenTextFile("text_file.txt", ForReading, False)
    Dim s As String
    s = ts.ReadAll()
End Sub

答案 1 :(得分:1)

如果您不想添加引用,可以直接使用vba代码。

例如以下文件 wordlist.txt

realize
empty
theorize
line
socialize
here
analyze

以下代码使用两种方法来执行(一种比另一种更常见):

Option Explicit

Sub main()
    Dim sFileName As String
    Dim sMergedLineArray() As String
    Dim sTextToFind As String
    Dim sReplacementText As String
    Dim sOutputFile As String
    Const MY_DELIMITER = "|"

    sFileName = "C:\deleteme\wordlist.txt"

    sMergedLineArray = ReadFileIntoArray(sFileName)

    sTextToFind = "ze"
    sReplacementText = "se"

    'Loop through each value in the array and make a change if you need to
    Dim x As Integer
    For x = 0 To UBound(sMergedLineArray)
        If InStr(1, sMergedLineArray(x), sTextToFind, vbTextCompare) > 0 Then
            sMergedLineArray(x) = Replace(sMergedLineArray(x), sTextToFind, sReplacementText, 1, -1, vbTextCompare)
        End If
    Next x

    sOutputFile = "C:\deleteme\UK_Version.txt"
    If Not SpitFileOut(sOutputFile, sMergedLineArray) Then
        MsgBox "It didn't work :("
    End If


    'OR...put it all together, make a mass change and split it back out (this seems unlikely, but throwing it in there anyway)
    sTextToFind = "se"
    sReplacementText = "ze"
    Dim sBigString As String
    Dim sNewArray As Variant
    sBigString = Join(sMergedLineArray, MY_DELIMITER)
    sBigString = Replace(sBigString, sTextToFind, sReplacementText, 1, -1, vbTextCompare)

    sNewArray = Split(sBigString, MY_DELIMITER, -1, vbTextCompare)

    sOutputFile = "C:\deleteme\American_Version.txt"
    If Not SpitFileOut(sOutputFile, sNewArray) Then
        MsgBox "It didn't work"
    End If

    MsgBox "Finished!"

End Sub


Function ReadFileIntoArray(sFileName As String) As String()
    Dim sText As String
    Dim sLocalArray() As String
    Dim iFileNum As Integer
    Dim iLineCount As Integer

    iFileNum = FreeFile

    Open sFileName For Input As #iFileNum
    Do Until EOF(iFileNum)
        Input #iFileNum, sText
        ReDim Preserve sLocalArray(iLineCount)
        sLocalArray(iLineCount) = sText
        iLineCount = iLineCount + 1
    Loop

    Close #iFileNum

    ReadFileIntoArray = sLocalArray


End Function


Function SpitFileOut(sFileName As String, sMyArray As Variant) As Boolean
    Dim iFileNum As Integer
    Dim iCounter As Integer

    SpitFileOut = False

    iFileNum = FreeFile

    Open sFileName For Output As #iFileNum
        For iCounter = 0 To UBound(sMyArray)
            Print #iFileNum, sMyArray(iCounter)
        Next
    Close #iFileNum


    SpitFileOut = True
End Function

如果你运行main sub,你最终会得到两个文件:

  • UK_Version.txt :这是第一种方法的结果
  • American_Version.txt :这是第二次
  • 的结果

VBA的第一课,年轻的Padawan;吸收它,学习并更改你的登录名:P