Re: [算表] MID一問--分開取出第n個被區隔的字串(VBA)

看板Office作者 (windknife18)時間17年前 (2009/04/08 00:02), 編輯推噓4(402)
留言6則, 3人參與, 最新討論串1/2 (看更多)
請參考囉,沒有很認真的做錯誤處理就是了 Sub marco1() Dim index As Integer Dim row As Integer row = 1 While Cells(row, 1).Value <> "" index = 1 While Nth_String(Cells(row, 1).Value, ".", index) <> "" Cells(index, row + 1).Value = Nth_String(Cells(row, 1).Value, ".", index) index = index + 1 Wend row = row + 1 Wend MsgBox ("成功") End Sub Function Nth_String(source As String, delimit As String, nth As Integer) As String Dim pre_pos, pos, cout As Integer pre_pos = 1 pos = 1 Count = 0 While (pos > 0) And (Count <> nth) pre_pos = pos pos = InStr(pos + 1, source, delimit) Count = Count + 1 Wend If (pos > 0) Then If (Count = 1) Then Nth_String = Left(source, pos - 1) Else Nth_String = Mid(source, pre_pos + 1, pos - pre_pos - 1) End If Else If (Count = nth) Then Nth_String = Mid(source, pre_pos + 1) End If End If End Function ※ 引述《JieJuen (David)》之銘言: : C1 : =MID(String,Start,End-Start) : C欄定義 : Delim ="." : End =FIND(X,SUBSTITUTE(Delim&String&Delim,Delim,X,第幾筆+1))-1 : Start =FIND(X,SUBSTITUTE(Delim&String&Delim,Delim,X,第幾筆)) : String =INDEX(Sheet1!$A:$A,COLUMN(Sheet1!A:A)) : X ="|" : 第幾筆 =ROW() : 在這題 Delimiter區隔字元是 "." : 假設絕不會出現在內容中的符號X 是 "|" : String是必須在C欄定義的原因 : 為了不連在一起(看比較清楚) 所以從C欄開始 : 去掉錯誤值的話 : =IF(COUNT(Start+End),MID(String,Start,End-Start),"") : http://p.2yu.co.cc/MidPro.xls : ※ 引述《xemerald ()》之銘言: : : 軟體:excel : : 版本:2003 : : #193mJTYm 文章第7528 : : 老實說看的有點難懂XD : : 不知道我能不能問一下基礎一點的東西再仔細研究 : : 儲存格 : : A B C D : : 1 A.B.C.D A AB D : : 2 AB.C.XW.HF B C E : : 3 D.E.HE C XW HE : : 4 D HF : : 假設1 每個想要被區隔的字都用 . 格開 : : 假設2 先假設每個筆資料最多兩個字 : : 那這樣要怎麼分析A : : 讓B C D能夠自動剖析資料呢 : : 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.229.78.201

04/08 00:33, , 1F
這...對我來說跳級很大XD 但一定大推的! 兩個我都欣賞!
04/08 00:33, 1F

04/08 04:52, , 2F
推!原來工作表的Find是vba的InStr 直觀但需一點功力!另外
04/08 04:52, 2F

04/08 04:54, , 3F
沒記錯的話Dim a, b, c As Integer 只有c明確定為整數
04/08 04:54, 3F

04/08 05:06, , 4F
感謝特別寫個函數出來 混在一起可能好寫但是難改 ^^
04/08 05:06, 4F

04/08 07:09, , 5F
幫附檔 http://p.2yu.co.cc/MidProVBA.xls 加自訂函數用法
04/08 07:09, 5F

04/08 09:17, , 6F
哈! 現在常用別的語言在寫程式,忘了要分開說 ^_^
04/08 09:17, 6F
文章代碼(AID): #19staFXY (Office)
文章代碼(AID): #19staFXY (Office)