【发布时间】: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 突出显示该行
我没有把所有东西都放在这里,比如尺寸标注等,因为我必须用手机才能访问这个网站,所以我再次用手机编写所有代码。
编辑:我添加了更多行,当我按下调试时,问题突出显示了刷新后台查询行。 我实际上是试图通过调整我的问题来实现这一点:
【问题讨论】:
-
你似乎没有问过问题。当前结果是什么?您希望该结果有何不同?
-
我根本无法得到结果。当我尝试运行它时,它会给出运行时错误“1004”。结果,我想从示例文件中获取我指出的值,然后将其写入单元格中。
-
文件中是否存在列迭代?您可以使用查询来驱动您的表,而不是整个文件,
where iterations=827您的错误是什么,它发生在哪里。 -
我只是添加了更多的行并在底部写了一些东西,编辑部分。我从另一个堆栈溢出问题中获取了代码,但无法使其适合我的情况。