【问题标题】:Reading a specific part of a text line using VBA使用 VBA 读取文本行的特定部分
【发布时间】:2019-01-28 10:31:41
【问题描述】:

我正在尝试从文本文件中获取第 827 行以写入单元格。有很多这样的文件,所以这就是我尝试使用宏的原因。文本文件如下所示

"Drag Convergence"
"Iterations" "cd"
1     7.74776e-01
2     6.51021e-01
3     5.58885e-01
.....
824     3.57617e-01
825     3.57617e-01

我只想将数字“3.57617e-01”写入一个单元格。我可以自己进行单元格排列,但我没有一个好的方法来读取该值,然后将其写入单元格让我们说 (1,1)

我的文件位置是

strFile = "D:\Analiz\Database\NACA63220_" & Mach(k) & Alpha(j) & Letter(i) & ".txt"

我所做的是使用

strPath = "D:\Analiz\Database\"
strExt = ".txt"
strSection = "Lift Convergence"
strValue = "825     "

With shtResult
     .Cells(1,1).Value = strValue
End With

strFile = "D:\Analiz\Database\NACA63220_" & Mach(k) & Alpha(j) & Letter(i) & ".txt"

Set data=shtSource.QueryTables.Add(Connection:TEXT;" & strPath & strFile, Destination:=shtSource.Cells(1, 1))

With data
.TextFileStartRow = 1
.TextFileParseType = xkDelimited
.TextFileVonsecutiveDelimeter = False
.TextFileTabDelimiter = False
.Text FileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True

.Refresh BackgroundQuery:=False
End With

Set fndSection = data.ResultRange.Find(strSection)
Set fndValue = data.ResultRange.Find(strValue, fndSection)
shtResult.Cells(shtResult.Rows.Count, 1).End(xkUp).Offset(1).Value = Replace(findValue, strValue, "")

这会产生运行时错误 1004,当我按下调试时,它会用 .Refresh BackgroundQuery 突出显示该行

我没有把所有东西都放在这里,比如尺寸标注等,因为我必须用手机才能访问这个网站,所以我再次用手机编写所有代码。

编辑:我添加了更多行,当我按下调试时,问题突出显示了刷新后台查询行。 我实际上是试图通过调整我的问题来实现这一点:

Importing data from multiple text files into Excel VBA

【问题讨论】:

  • 你似乎没有问过问题。当前结果是什么?您希望该结果有何不同?
  • 我根本无法得到结果。当我尝试运行它时,它会给出运行时错误“1004”。结果,我想从示例文件中获取我指出的值,然后将其写入单元格中。
  • 文件中是否存在列迭代?您可以使用查询来驱动您的表,而不是整个文件,where iterations=827 您的错误是什么,它发生在哪里。
  • 我只是添加了更多的行并在底部写了一些东西,编辑部分。我从另一个堆栈溢出问题中获取了代码,但无法使其适合我的情况。

标签: excel vba


【解决方案1】:

使用此函数从文本文件中读取特定行

    ' read specific line no.
    ' RESULT : searched text (string)
    Function ReadLine(FilePath, LineNumber) As String
    Dim i As Integer, count As Long
    Dim strLine As String
    i = FreeFile
    Open FilePath For Input As #i
    count = 0

    While Not EOF(i)
        Line Input #i, strLine
        count = count + 1
        If count = LineNumber Then
            ReadLine = strLine
            Close #i
            Exit Function
        End If
    Wend

Close i
End Function

您在实施方面还有其他问题吗?请澄清

第二个代码用于字符串拆分

     Function BreakString(a As String, pos As Integer) As String
     Dim WrdArray() As String
     WrdArray() = Split(a, "     ")
     BreakString = WrdArray(pos)
     End Function

Dim Text, Part1, Part2 As String
Text = "827     3.57617e-01"
Part1 = BreakString(CStr(Text), 0)
Part2 = BreakString(CStr(Text), 1)

【讨论】:

  • 问题是,我需要省略数字 827 和 5 个空格,而只需获取后面的数字。我认为你的代码得到了整行?
  • 是的,整行,你可以使用简单的条件,比如:如果 ReadLine StartWith 827, Split by [space], 1, 最后 Trim the string.. 可以转换为 Double (CDbl)
  • 我理解你的代码,但你能举个关于修剪的例子吗?我检查了它,但它是否削减了代码的最后一部分?
  • 看我的下一篇文章
  • 是的,通过将这些放在一起,它起作用了!谢谢你,我接受了这个作为答案。我认为,如果您将第二个代码添加到此代码中,对于可能会发现此问题的其他人来说会更好。
【解决方案2】:

我建议您不要为此使用标准的 VBA 文件处理:VBA 文件处理从第一行开始读取文件,然后逐行继续,直到到达您要查找的行。由于您需要阅读 827 行,因此您等待 827 时间的时间太长了。

而不是这个,我会尝试找到一个可以处理这个的命令行,比如:

tail -1 <filename> >output.txt (or head -827 <filename> | tail -1 >output.txt)

将其翻译成 Excel VBA,类似于:

Shell "tail -f <filename> >output.txt" //pseudo-code

然后,使用 VBA 标准文本处理来读取 output.txt。

【讨论】:

  • 我也会检查一下,上述解决方案有效,所以我会接受它作为答案,但谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2016-12-18
  • 2021-10-22
相关资源
最近更新 更多