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

このコードは、テキスト ファイルを正常に分割し、必要な 2 つの値を 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 と 2 が必要strSNOですか? VBScript 配列は 0 ベースであるため、最初のインデックスは 1 ではなく 0 です。

エラーを特定するには、デバッグ コードを追加します。

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

for someがstrAll(i)含まれていないことが問題であることが判明した場合、 は1 つの要素だけを含む配列を生成します。次のような方法で回避できます。|iSplit()

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