Re: [算表] MID一問--分開取出第n個被區隔的字串(VBA)
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
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Office 近期熱門文章
PTT數位生活區 即時熱門文章