Sub TEST()
Dim xPath$, xF$, xS As Worksheet, xEnd As Range, xR As Range, TT, T, N$, C%
xPath = ThisWorkbook.Path & "\"
Set xS = ThisWorkbook.Sheets("工作表1")
Do
If xF = "" Then xF = Dir(xPath & "*.txt") Else xF = Dir
If xF = "" Then Exit Do
If Not xS.[A:A].Find(xF, LookAT:=xlWhole) Is Nothing Then GoTo 101
Set xEnd = xS.Cells(Rows.Count, 1).End(xlUp)(2)
If xEnd.Row < 3 Then Set xEnd = xS.[A3]
xEnd = xF
Open xPath & xF For Input Access Read As #1
Do Until EOF(1)
Line Input #1, T
N = Switch(T = "&", "B", T = "%", "M", T = "$", "AC", T = T, "")
'_T="&",取B欄,類推∼∼;找不到"&%$",N為空值
If N <> "" Then Set xR = xEnd(1, N): C = 0
'_找到"&%$"後,以xR定位為各分類的首格
For Each TT In Split(T, " ")
C = C + 1: xR(1, C) = TT
Next
Loop
Close #1
101: Loop
End Sub
大致如上,其他細節請自行更改或調整∼∼作者: 准提部林 時間: 2015-10-28 11:21
本帖最後由 准提部林 於 2015-10-28 11:25 編輯
N = Switch(T = "&", "B", T = "%", "M", T = "$", "AC", T = T, "")
也可用:
N = Array("", "", "B", "M", "AC")(InStr("_&%$", T))
_InStr 值只有0,1,2,3,4 五種結果
感謝 准大 果然厲害
但請問
If Not xS.[A:A].Find(xF, LookAT:=xlWhole) Is Nothing Then GoTo 101
Set xEnd = xS.Cells(Rows.Count, 1).End(xlUp)(2)
If xEnd.Row < 3 Then Set xEnd = xS.[A3]
xEnd = xF
- - - - - -
For Each TT In Split(T, " ")
C = C + 1: xR(1, C) = TT
Next
個別是什麼意思? 使用說明看不太懂
能請准大為小弟稍微解釋一下嗎 謝謝作者: 准提部林 時間: 2015-10-29 18:02
Do Until EOF(1)
Line Input #1, T
If T = "&" Then Set xR = xEnd(1, "B"): C = 0
If (T = "#" Or T = "$") And N = "" Then Set xR = xEnd(1, "M"): C = 0: N = "Y"
If T = "@" Then Set xR = xEnd(1, "AO"): C = 0
For Each TT In Split(T, " ")
C = C + 1: xR(1, C) = TT
Next
Loop作者: Jason80Lo 時間: 2015-11-3 23:03
If (T = "#" Or T = "$") And N = "" Then Set xR = xEnd(1, "M"): C = 0: N = "Y"
准大請問這行的意思,xEnd(1, "M")裡面的1什麼意思?
小弟試另三個TXT檔,同時開啟後只有第一個能夠順利依照儲存格編排(如下圖)
If (T = "#" Or T = "%") And N = "" Then Set xR = xEnd(1, "FZ"): C = 0: N = "Y"
If (T = "#" Or T = "$") And N = "" Then Set xR = xEnd(1, "M"): C = 0: N = "Y"
_當遇"#"或"$",即以最後列的M欄為填入資料的起始格,
因"#"與"$"個數不一定,且時有時無,又位置沒有既定順序,
所以,只要遇到第一個"#"或"$",即以N="Y"表示已取得起始格,其後的就算是累計個數∼∼
回復 11#准提部林
If (T = "#" Or T = "%") And N = "" Then Set xR = xEnd(1, "FZ"): C = 0: N = "Y"
不好意思准大,我的TXT檔規則都是與第一個一樣,目前的問題是開啟第一個TXT檔排列都OK,
但第一個以後的遇到#、%的排序就無法從FZ儲存格填入。作者: Jason80Lo 時間: 2015-11-15 14:08
准大 我將你的方法套入
If (T = "#" Or T = "%") And N = "Open xPath & xF For Input Access Read As #1" Then Set xR = xEnd(1, "N"): C = 0: N = "Y"
但是顯示出來的排列"#" 沒有依照我要的從N欄位開始依序排列,而是直接依序欄位排列下來,請問是小弟哪裡打錯@@作者: 准提部林 時間: 2015-11-17 21:47
Sub TEST()
Dim xPath$, xF$, xS As Worksheet, xEnd As Range, xR As Range, TT, T, N$, C%
xPath = ThisWorkbook.Path & "\" '"C:\Users\j\Desktop\VBA TXT 依照段落開啟需求\"
Set xS = ThisWorkbook.Sheets("工作表1")
Do
If xF = "" Then xF = Dir(xPath & "\*.txt") Else xF = Dir
If xF = "" Then Exit Do
If Not xS.[A:A].Find(xF, LookAT:=xlWhole) Is Nothing Then GoTo 101
Set xEnd = xS.Cells(Rows.Count, 1).End(xlUp)(2)
If xEnd.Row < 3 Then Set xEnd = xS.[A3]
xEnd = xF
N = ""
Open xPath & xF For Input Access Read As #1
Do Until EOF(1)
Line Input #1, T
If T = "&" Then Set xR = xEnd(1, "B"): C = 0
If T = "$" Then Set xR = xEnd(1, "BG"): C = 0
If (T = "#" Or T = "%") And N = "" Then Set xR = xEnd(1, "FZ"): C = 0: N = "Y"
If T = "@" Then Set xR = xEnd(1, "DX"): C = 0
For Each TT In Split(T, " ")
C = C + 1: xR(1, C) = TT
Next
Loop
Close #1
101: Loop
End Sub作者: Jason80Lo 時間: 2015-11-17 23:35