尝试在VBA中存储文本文件行 [英] trying to store text file rows in VBA

查看:165
本文介绍了尝试在VBA中存储文本文件行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

查看FileSystemObject(ref: 1 2 3



您必须转到菜单并包含 Microsoft Scripting Runtime 并创建一个全局变量 Global fso as New FileSystemObject 。现在,您的代码中的任何地方都可以执行 fso.OpenTextFile(),返回一个 TextStream 。每个TextStream都有方法loke ReadLine() ReadAll() SkipLine() code>, WriteLine()等...





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

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


Greetings, I'm hoping for help in figuring out how to store each row of a text file read into a VBA program as a string. I want to modify one of the strings and then put them all back together, but do not know how to read through a text file and store each row as a separate variable in an intelligent way. Thanks for any help you can provide!

解决方案

Look into the FileSystemObject (ref: 1, 2, 3)

You have to go to menu and include the Microsoft Scripting Runtime and create a global variable Global fso as New FileSystemObject. Now anywhere in your code do things like fso.OpenTextFile() which returns a TextStream. Each TextStream has methods loke ReadLine(), ReadAll(), SkipLine(), WriteLine(), etc ...

Here is a quick sample code.

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

这篇关于尝试在VBA中存储文本文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆