การเชื่อมบรรทัดจากไฟล์ข้อความ - Excel VBA

ฉันมีไฟล์ข้อความในรูปแบบเช่นนั้น

ข้อความ:

-- Begin 
  Line1
  Line2
  Line3
      ^
-- Begin
  Line1
  Line2
  Line3
  Line4
      ^
.
.
.
.

โดยพื้นฐานแล้วฉันต้องการใส่ Line1 ถึง Line(whatever) ระหว่างบรรทัด --Begin ถึง ^ ในอาร์เรย์ ดังนั้นแต่ละองค์ประกอบในอาร์เรย์จึงเป็นกลุ่มบรรทัด ตัวอย่างอาร์เรย์

Array =  [("Line1"  & vbNewLine & "Line 2") , ("Line1"  & vbNewLine & "Line 2" & vbNewLine & "Line 3") ... ]

แต่โดยพื้นฐานแล้วต้องการเก็บแต่ละองค์ประกอบในอาร์เรย์ไว้ในเซลล์ (อาจไม่จำเป็นต้องใช้อาร์เรย์ด้วยซ้ำ) ...

ไม่แน่ใจว่าจะเป็นไปได้ใน excel VBA หรือไม่ แต่นี่คือสิ่งที่ฉันได้ลองมาแล้ว

    Dim FileNum As Integer
    Dim DataLine As String
    Dim Lines As Variant
    Dim j As Integer


    FileNum = FreeFile()
    Open "C:..." For Input As #FileNum


 While Not EOF(FileNum)
       Line Input #FileNum, DataLine

      If InStr(DataLine, "-- Begin") > 0 Then
        nextLinecounter = 1

      ElseIf InStr(DataLine, "^") > 0 Then
        nextLinecounter = 0
        j = j + 1

      ElseIf nextLinecounter = 1 Then
       Lines(j) = DataLine + .. Somehow concatenate next lines into array

     End If


Wend

ฉันติดอยู่กับวิธีข้ามบรรทัดถัดไปแล้วผนวกเข้ากับรายการปัจจุบัน ทำอย่างไรก็ได้ขอบคุณ


person Thatdude1    schedule 22.10.2013    source แหล่งที่มา
comment
@mehow ฉันจะใช้ readAll และกำหนดขอบเขตว่าจะเริ่มต้นและหยุดอ่าน all ได้อย่างไร   -  person Thatdude1    schedule 22.10.2013


คำตอบ (1)


ดังนั้นฉันจะทำให้มันแตกต่างออกไปเล็กน้อย ใช้วิธีการที่ทันสมัยมากขึ้นในการอ่านไฟล์

ดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีการอ่านไฟล์ *.txt ใน VBA - here

หมายเหตุ: คุณต้องเพิ่มการอ้างอิงถึง Microsoft Scripting Runtime ผ่าน VBE -> เครื่องมือ -> การอ้างอิง

Option Explicit

Sub ReadTxtFile()

    Dim oFSO As New FileSystemObject
    Dim oFS As TextStream

    Dim filePath As String
    filePath = "C:\Users\" & Environ$("username") & "\Desktop\foo.txt"

    If Not fileExist(filePath) Then GoTo FileDoesntExist

    On Error GoTo Err

    ReDim arr(0) As String
    Dim s As String

    Set oFS = oFSO.OpenTextFile(filePath)
    Do While Not oFS.AtEndOfStream
        Dim line As String
        line = oFS.ReadLine
        If InStr(line, "-- Begin") = 0 And InStr(line, "^") = 0 Then
            s = s & line
        End If
        If InStr(line, "^") > 0 Then
            arr(UBound(arr)) = s
            ReDim Preserve arr(UBound(arr) + 1)
            s = vbNullString
        End If
    Loop
    ReDim Preserve arr(UBound(arr) - 1)
    oFS.Close

    Dim k As Long
    For k = LBound(arr) To UBound(arr)
        Debug.Print k, arr(k)
    Next k
    Exit Sub

FileDoesntExist:
    MsgBox "File Doesn't Exist", vbCritical, "File not found!"
    Exit Sub

Err:
    MsgBox "Error while reading the file.", vbCritical, vbNullString
    oFS.Close
    Exit Sub

End Sub


Function fileExist(path As String) As Boolean
    fileExist = IIf(Dir(path) <> vbNullString, True, False)
End Function

foo.txt มีลักษณะเช่นนี้

-- Begin 
  Line1
  Line2
  Line3
      ^
-- Begin
  Line1
  Line2
  Line3
  Line4
      ^

อาร์เรย์ของคุณมีลักษณะเช่นนี้

ป้อนคำอธิบายรูปภาพที่นี่

person Community    schedule 22.10.2013
comment
เพราะสิ่งนี้ไม่มี arr หนึ่งบรรทัดในแต่ละองค์ประกอบ .. มีวิธีจัดเก็บหลายบรรทัดในแต่ละองค์ประกอบหรือไม่? - person Thatdude1; 22.10.2013
comment
หากฉันกำลังอ่านมันหลังจากองค์ประกอบต่างๆ มากมาย จะมีองค์ประกอบที่มีหลายบรรทัดอยู่... ดังนั้นในไฟล์ .txt ของคุณ arr(4) = Line1 Line2 Line3 ? - person Thatdude1; 22.10.2013
comment
และฉันไม่คิดว่ามันจะได้ผล .. arr(UBound(arr)) & vbNewLine .. ผนวกทุกบรรทัดก่อนหน้า or ? - person Thatdude1; 22.10.2013
comment
ฉันแก้ไขโค้ดแล้วเพื่อให้แทรกแต่ละบล็อกของ txt ที่ดัชนีแยกกัน นี่คือสิ่งที่คุณต้องการใช่ไหม? - person ; 22.10.2013
comment
@Thatdude1 คุณหมายถึงอะไร i dont think this works? อะไรไม่ได้ผลสำหรับคุณ? - person ; 22.10.2013
comment
ขอบคุณมากที่สละเวลา ฉันทำมันสำเร็จแล้ว และใช่ นั่นคือสิ่งที่ฉันต้องการ... ไชโย! - person Thatdude1; 22.10.2013
comment
วุ้ย.. ฉันเริ่มกังวลอย่างจริงจังแล้ว :P ฉันดีใจที่ได้ผล - person ; 22.10.2013