2015-11-14 25 views
0

[輸入鏈接說明] [1]我試圖搜索此文本文件中的特定單詞,以便在excel列中輸出行內容。該文本文件包含多個部分。我能夠輸出我的文本文件的第一部分,但出於某些原因,我無法定義循環,因此我可以檢索文件的每個部分。文本文件中的精確信息,使用VBA優化列

我迄今爲止代碼:

Sub test() 
Dim myFile As String, text As String, textline As String, DDC As Integer, DDR As Integer, DDP As Integer, ADC As Integer, i As Integer, SE As Integer, SP As Integer, SG As Integer, j As Integer, v As Integer 

myFile = "C:\Users\Seb\Desktop\text2.txt" 
Open myFile For Input As #1 
Do Until EOF(1) 
Line Input #1, textline 
text = text & textline 
Loop 
Close #1 

i = 1 

    DDC = InStr(text, "Date de calcul") 
    DDR = InStr(text, "Date de retraite") 
    ADC = InStr(text, "Âge à la date du calcul") 
    SE = InStr(text, "Service d'emploi") 
    SP = InStr(text, "Service de participation") 
    SG = InStr(text, "Salaire gagné") 
    Cells(i + 1, 1).Value = Mid(text, DDC, 14) 
    Cells(i + 1, 2).Value = Mid(text, DDC + 36, 10) 
    Cells(i + 2, 1).Value = Mid(text, DDR, 16) 
    Cells(i + 2, 2).Value = Mid(text, DDR + 36, 10) 
    Cells(i + 3, 1).Value = Mid(text, ADC, 23) 
    Cells(i + 3, 2).Value = Mid(text, ADC + 36, 6) 
    Cells(i + 4, 1).Value = Mid(text, SE, 16) 
    Cells(i + 4, 2).Value = Mid(text, SE + 36, 6) 
    Cells(i + 5, 1).Value = Mid(text, SP, 24) 
    Cells(i + 5, 2).Value = Mid(text, SP + 36, 6) 
    For v = 0 To 10 
    j = v * 228 
    Cells(v + 7, 1).Value = Mid(text, SG + j, 24) + Mid(text, SG + 64 + j,  10) + "/ " + Mid(text, SG + 77 + j, 10) 
    Cells(v + 7, 2).Value = Mid(text, SG + 103 + j, 10) 
    Next v 

End Sub 

我的文本文件的爲例,請訪問:http://txt.do/5j2dq

正如我前面提到的,我只能在Excel輸出部分1。我的代碼應該是爲了檢索我的文本文件的每個部分?

+0

如何回到你以前的問題? –

回答

0

在您完成每個部分後,只需從text字符串中刪除覆蓋部分,以便在下一次迭代中,例如InStr(text, "Date 1")將找到下一節的Date 1行。

Do While True 
    DDC = InStr(text, "Date 1") 
    If DDC = 0 Then 
     ' no more sections - exit loop 
     Exit Do 
    End If 
    DDR = InStr(text, "Date 2") 
    ADC = InStr(text, "Age") 
    ' ...... 

    Next v 

    ' remove the section that was just handled 
    text = Mid(text, SG + 30) 
Loop 
+0

我不明白這部分代碼如何刪除一個部分'刪除剛剛處理的部分 text = Mid(text,SG + 30) – mckaymental

+0

@mckaymental:在單步執行代碼時只需嘗試一下。 「SG」指向「薪水」線的開始,「SG + 30」即將結束。沒有第三個參數的'Mid()'從第二個參數開始一直到結尾爲止。 – Andre

+0

它可以工作,但我得到的第一個數據是由Excel中的第二組擦除。另外我的文件中有很多這樣的部分,我怎樣才能把它們全部弄清楚?我在這裏http://txt.do/5j2dq提供了一個真實文件的擴展名 – mckaymental

0

如果你把txt文件中的數據►外部數據►從文本,你可以(作爲一個是真有連續分隔符號)區間作爲一個其他分隔符。

Sub Import_Text() 
    Dim c As Long, myFile As String 

    myFile = "C:\Users\Seb\Desktop\text.txt" 

    With Worksheets("Sheet9") '<~~set this worksheet reference properly! 

     With .QueryTables.Add(Connection:="TEXT;" & myFile, _ 
      Destination:=Range("$A$1")) 
      .Name = "TXT" 
      .FieldNames = True 
      .RowNumbers = False 
      .FillAdjacentFormulas = False 
      .PreserveFormatting = True 
      .RefreshOnFileOpen = False 
      .RefreshStyle = xlInsertDeleteCells 
      .SavePassword = False 
      .SaveData = True 
      .AdjustColumnWidth = True 
      .RefreshPeriod = 0 
      .TextFilePromptOnRefresh = False 
      .TextFilePlatform = 437 
      .TextFileStartRow = 1 
      .TextFileParseType = xlDelimited 
      .TextFileTextQualifier = xlTextQualifierDoubleQuote 
      .TextFileConsecutiveDelimiter = True 
      .TextFileTabDelimiter = False 
      .TextFileSemicolonDelimiter = False 
      .TextFileCommaDelimiter = False 
      .TextFileSpaceDelimiter = False 
      .TextFileOtherDelimiter = "." 
      .TextFileColumnDataTypes = Array(1, 1) 
      .TextFileTrailingMinusNumbers = True 
      .Refresh BackgroundQuery:=False 
     End With 

     'these will cleanup (trim) the results 
     For c = 1 To 2 
      With .Columns(c) 
       .TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _ 
           FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True 
      End With 
     Next c 

    End With 
End Sub 

有兩個最終Range.TextToColumns methodxlFixedWidth選項,只是從結果剪掉任何流氓領先/欄杆空間。

相關問題