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

看板Office作者 (windknife18)時間17年前 (2009/04/15 13:12), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串2/2 (看更多)
J大提到要我先修改,所以拿來給囉 ^_^ 不過其實通常解過的題目,都希望對方可以自己改就是了 ... 也就是所謂的「給對方魚、不如給對方釣竿囉」 ^_^ 請看看以下程式是不是你要的囉,不確定你的出現 "A" 是指字串中有出現就算, 還是一定要是 "A" ,所以就先用一定是 "A" 來解, 如果是要在字串中有出現就算的話,就改成 instr 就可以了! 通常你只要能講的出規則其實都可以嘗試去練習寫囉 ... Option Explicit '作者:windknife18.bbs@ptt.cc Sub Marco1() Dim index As Integer, index2 As Integer Dim row As Integer row = 1 While Cells(row, 1) <> "" index = 1 index2 = 1 Columns(row + 2).ClearContents While Nth_String(Cells(row, 1), ".", index) <> "" If Cells(1, 2) = "1" Then If Nth_String(Cells(row, 1), ".", index) = "F" Then Cells(index2, row + 2) = "WH" Else Cells(index2, row + 2) = Nth_String(Cells(row, 1), ".", index) End If ElseIf Cells(1, 2) = "2" Then If Nth_String(Cells(row, 1), ".", index) = "A" Then Cells(index2, row + 2) = "AR" Else Cells(index2, row + 2) = Nth_String(Cells(row, 1), ".", index) End If ElseIf Cells(1, 2) = "3" Then If Nth_String(Cells(row, 1), ".", index) = "A" Then Cells(index2, row + 2) = "AR" index2 = index2 + 1 Cells(index2, row + 2) = "P" Else Cells(index2, row + 2) = Nth_String(Cells(row, 1), ".", index) End If Else Cells(index2, row + 2) = Nth_String(Cells(row, 1), ".", index) End If index = index + 1 index2 = index2 + 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 As Integer, pos As Integer, count 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 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.131.84.79

04/15 22:40, , 1F
酷呀^^ 推!
04/15 22:40, 1F

04/15 23:42, , 2F
^^
04/15 23:42, 2F
文章代碼(AID): #19vMpIkM (Office)
文章代碼(AID): #19vMpIkM (Office)