【问题标题】:loading Data in VBA from a text file从文本文件加载 VBA 中的数据
【发布时间】:2010-05-21 18:50:25
【问题描述】:

我对 VBA 不是很熟悉,但需要将它用于我正在使用的新软件程序(与 Microsoft 无关)

我有一个文本文件,其中包含我想读入 VBA 的数据列。

具体来说,文本文件每行有 4 个条目。因此我想加载列向量(N x 1)。

文本文件由每个条目之间的空格分隔。

例如,我想加载第一列并将其保存为数组 A,然后将第二列保存为数组 B,然后将第三列保存为数组 C,然后将第四列保存为数组 D。

下面从http://www.tek-tips.com/faqs.cfm?fid=482 找到的这段代码 sn-p 是我发现可以将文本加载到数组中的东西,但我需要对其进行调整,以便能够将列保存为上面指定的不同数组...

Open "MyFile.txt" For Input As #1
ReDim Txt$(0)
Do While Not EOF(1)
ReDim Preserve Txt$(UBound(Txt$) + 1)
Input #1, Txt$(UBound(Txt$))
Loop
Close #1

【问题讨论】:

  • 将列保存为变量是什么意思?是否要将这些列存储为数组?

标签: file vba text load


【解决方案1】:

对于本示例,您需要一个名为 schema.ini 的文件,该文件与文本文件位于同一目录中。它应该包含:

[Import.txt]
Format=Delimited( )

其中 Import.txt 是文件名 (http://msdn.microsoft.com/en-us/library/ms709353(VS.85).aspx)。

然后您就可以使用它了,它应该可以在 VBScript 或 VBA 中工作,并且几乎不会被篡改:

Set cn = CreateObject("ADODB.Connection")

'Note HDR=Yes, that is, first row contains field names '
'and FMT delimted, ie CSV '

strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Docs\;" _
& "Extended Properties=""text;HDR=Yes;FMT=Delimited"";"

cn.Open strcon

strSQL="SELECT * FROM Import.txt" _

set rs = createobject("adodb.recordset")

rs.open strSQL,cn

MsgBox rs(2)
MsgBox rs.GetString

第一个消息框应该返回第一行的第三列,这是一个有效的测试。

第二个消息框应该返回整个文件,所以不要将它与大集合一起使用。可以操作记录集,也可以使用 .GetRows 创建值数组 (http://www.w3schools.com/ado/met_rs_getrows.asp)

【讨论】:

    【解决方案2】:

    似乎剩下的问题是将一个行数组转换为四个列数组。 也许这个 sn-p 有帮助

    Option Explicit
    Option Base 0
    
    Sub import()
        Dim sTxt() As String
        Dim sLine As Variant
        Dim iCountLines As Long
        Dim iRowIterator As Long
        Dim i As Long
        Dim sRow() As String
        Dim sColumnA() As String
        Dim sColumnB() As String
        Dim sColumnC() As String
        Dim sColumnD() As String
    
        ' read in file '
        Open "MyFile.txt" For Input As #1
        ReDim sTxt(0)
    
        Do While Not EOF(1)
            Input #1, sTxt(UBound(sTxt))
            ReDim Preserve sTxt(UBound(sTxt) + 1)
        Loop
        Close #1
    
        ' dim array for each columns '
        iCountLines = UBound(sTxt)
        Debug.Print "working with ", iCountLines, "lines"
        ReDim sColumnA(iCountLines)
        ReDim sColumnB(iCountLines)
        ReDim sColumnC(iCountLines)
        ReDim sColumnD(iCountLines)
    
        ' "transpose" sTxt '
        iRowIterator = 0
        For Each sLine In sTxt
            sRow = Split(sLine, " ")
            If UBound(sRow) = 3 Then
                sColumnA(iRowIterator) = sRow(0)
                sColumnB(iRowIterator) = sRow(1)
                sColumnC(iRowIterator) = sRow(2)
                sColumnD(iRowIterator) = sRow(3)
                iRowIterator = iRowIterator + 1
            End If
        Next sLine
    
        ' now work with sColumnX '
        Debug.Print "Column A"
        For i = 0 To iCountLines
            Debug.Print sColumnA(i)
        Next i
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      您的问题中的细节很少,但我建议使用“文本到列”

      如果您对 VBA 编程不是很熟悉,请尝试使用以下步骤录制宏:

      1. 将文件导入 Excel
      2. 选择 A 列
      3. 选择“文本到列”表单工具菜单
      4. 选择以空格分隔

      这样您将获得您要求的数据数组,现在分配给您想要的任何变量都不成问题。

      编辑(不使用 Excel):

      看看FSO 方法。

      通过替换

      MsgBox 字符串

      具有某种拆分功能,例如

      strTemp = Split(strLine, " ")
      

      您将能够遍历源文件中的所有值,这可行吗?

      【讨论】:

      • 是的,它将是 VBA 中的一个数组......就像我说这与 Microsoft 无关,所以 Excel 中的任何内容都不会削减它......我想采用列向量 A、B、 C、D 的大小都是 N 乘 1,我将通过文本文件加载它们......然后我使用 for 循环对这些列向量进行处理,稍后该循环已经实现并且必须在 VBA 中完成,我只需要加载那些是数组的列向量..
      • hmmm 得到错误“期望现有的 scalar.var”(设置 objFSO = CreateObject("Scripting.FileSystemObject"))
      • 这对我来说并不熟悉:/ 你在 VBEditor 中工作吗?您是否添加了对项目的引用(工具>参考>添加>scrrun.dll)?
      • 我正在使用允许创建 VBA 宏的第 3 方软件包......
      • 我明白了...您提供的链接中的代码可以正常工作吗?
      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2021-01-11
      相关资源
      最近更新 更多