- 帖子
- 835
- 主題
- 6
- 精華
- 0
- 積分
- 915
- 點名
- 0
- 作業系統
- Win 10,7
- 軟體版本
- 2019,2013,2003
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2010-5-3
- 最後登錄
- 2024-11-14
|
2#
發表於 2015-2-25 22:21
| 只看該作者
本帖最後由 luhpro 於 2015-2-25 22:24 編輯
各位親愛的大大午安
有個小問題想要跟各位請益,
要如何將一個有好幾行資料的txt檔,匯出成每一行 ...
flowrew 發表於 2015-2-25 13:58 
僅就你提供的圖檔猜測文字檔的內容,試試看...- Sub nn()
- Dim iMode%, iBgn%, iCol%
- Dim lRow&
- Dim sStr$, sTemp$
- Dim vD
- Dim vFs, vF
-
- Cells.Clear
-
- Set vD = CreateObject("Scripting.Dictionary")
- lRow = 3
- iCol = 3
- iMode = 0
- Set vFs = CreateObject("Scripting.FileSystemObject")
- Set vF = vFs.OpenTextFile(ThisWorkbook.Path & "\123.txt", 1, -2)
- Do While Not vF.AtEndOfStream
- sStr = Trim(vF.ReadLine)
- If sStr <> "" Then
-
- Select Case iMode
- Case Is > 2 ' 資料區
- iBgn = InStr(1, sStr, " ")
- Cells(vD("1"), iCol) = Left(sStr, iBgn - 1)
-
- iBgn = InStr(iBgn, sStr, " ")
- GoSub SkipSpace
- Cells(vD("2"), iCol) = sTemp
-
- iBgn = InStr(iBgn, sStr, " ")
- GoSub SkipSpace
- Cells(vD("3"), iCol) = sTemp
-
- iBgn = InStr(iBgn, sStr, " ")
- GoSub SkipSpace
- Cells(vD("3") + 1, iCol) = sTemp
-
- iBgn = InStr(iBgn, sStr, " ")
- GoSub SkipSpace
- Cells(vD("4"), iCol) = sTemp
- iCol = iCol + 1
-
- Case 0 ' SN
- If InStr(1, sStr, "SN") > 0 Then
- Cells(lRow, 2) = "SN"
- Cells(lRow, 3) = Trim(Mid(sStr, InStr(1, sStr, ":") + 1, 10))
- iMode = iMode + 1
- lRow = lRow + 1
- End If
-
- Case 1 ' Model
- If InStr(1, sStr, "Model") > 0 Then
- Cells(lRow, 2) = "Model"
- Cells(lRow, 3) = Trim(Mid(sStr, InStr(1, sStr, ":") + 1, 10))
- iMode = iMode + 1
- lRow = lRow + 2
- End If
- Case 2 ' A B C D
- iBgn = InStr(1, sStr, " ")
- Cells(lRow, 2) = Left(sStr, iBgn - 1)
- vD("1") = lRow
-
- GoSub SkipSpace
- lRow = lRow + 1
- Cells(lRow, 2) = sTemp
- vD("2") = lRow
- iBgn = InStr(iBgn, sStr, " ")
-
- GoSub SkipSpace
- lRow = lRow + 2
- Cells(lRow, 2) = sTemp
- vD("3") = lRow
- iBgn = InStr(iBgn, sStr, " ")
-
- GoSub SkipSpace
- lRow = lRow + 2
- Cells(lRow, 2) = sTemp
- vD("4") = lRow
- iMode = iMode + 1
- End Select
- End If
- Loop
- vF.Close
-
- Exit Sub
- SkipSpace:
- Do While Mid(sStr, iBgn, 1) = " "
- iBgn = iBgn + 1
- Loop
- sTemp = Trim(Mid(sStr, iBgn, IIf(InStr(iBgn, sStr, " ") = 0, _
- Len(sStr) + 1, InStr(iBgn, sStr, " ")) - iBgn))
- Return
- End Sub
複製代碼
test.zip (10.16 KB)
|
|