1
Set objReadFile = objFSO.OpenTextFile(objFile.Path, ForReading)
    strAll = Split(objReadFile.ReadAll, vbCrLf, -1, vbTextCompare) 'Gets each line from file
    i = LBound(strAll)
    Do While i < UBound(strAll)
       If (InStr(1, strAll(i), "DAU SNo.-C0", vbTextCompare) > 0) Then
          i = i + 4 'Skip 4 lines to get to first SN
          Do Until InStr(1, strAll(i), "+", vbTextCompare) > 0 'Loop until line includes "+"
             strSNO = Split(strAll(i), "|", -1, vbTextCompare)
             'put strSNO into next cell in column A
             **objSheet.Cells.Offset(1,0).Value = Trim(strSNO(1))**
             i = i + 1
          Loop
       End If
    i = i + 1
    Loop

此代码成功拆分文本文件并将我想要的两个值放入 strSNO(1) 和 strSNO(2) 中。我想将它们写入 A 列第 2 行和 B 列第 2 行,然后在循环的下一次迭代中将下一个值放入第 3 行。我尝试了偏移方法,它给出了错误。我找到的所有帮助都是针对 VBA 的。谁能告诉我要在粗体字的地方放什么来修复它?

编辑:

解决了。​​这就是我所做的:

strAll = Split(objReadFile.ReadAll, vbCrLf, -1, vbTextCompare) 'Gets each line from file
    i = LBound(strAll)
    c=2
    Do While i < UBound(strAll)
       If (InStr(1, strAll(i), "DAU SNo.-C0", vbTextCompare) > 0) Then
          i = i + 4 'Skip 4 lines to get to first SN
          Do Until InStr(1, strAll(i), "+", vbTextCompare) > 0 'Loop until line includes "+"
            strSNO = Split(strAll(i), "|", -1, vbTextCompare) 
            i = i + 1
            objSheet.Cells(c,1).Offset(1,0).Value = Trim(strSNO(1))
            objSheet.Cells(c,2).Offset(1,0).Value = Trim(strSNO(2))
            c=c+1
            Loop
      End If
    i = i + 1
    Loop
4

1 回答 1

0

代替

objSheet.Cells.Offset(1,0).Value = Trim(strSNO(1))

objSheet.Cells(i,1).Value = Trim(strSNO(1))
objSheet.Cells(i,2).Value = Trim(strSNO(2))

编辑:你确定你想要字段 1 和 2strSNO吗?VBScript 数组是从 0 开始的,所以第一个索引是 0,而不是 1。

要定位错误,请添加一些调试代码:

On Error Resume Next
objSheet.Cells(i,1).Value = Trim(strSNO(1))
If Err Then
  WScript.Echo i & ": " & strAll(i)
  WScript.Echo "strSNO(1) = " & strSNO(1)
  WScript.Echo "strSNO(1) is of type " & TypeName(strSNO(1))
End If
Err.Clear
objSheet.Cells(i,2).Value = Trim(strSNO(2))
If Err Then
  WScript.Echo i & ": " & strAll(i)
  WScript.Echo "strSNO(2) = " & strSNO(2)
  WScript.Echo "strSNO(2) is of type " & TypeName(strSNO(2))
End If
On Error Goto 0

如果问题结果是strAll(i)不包含 a |for some i,因此Split()生成一个只有一个元素的数组,您可以通过以下方式解决该问题:

strSNO = Split(strAll(i) & "|", "|", -1, vbTextCompare)
于 2013-03-06T21:46:38.050 回答