Sub Sample2()
Dim buf As String, n As Long
buf = "ABC,DEF,GHI"
n = InStr(buf, ",")
If n > 0 Then
MsgBox n & "文字目に存在します"
Else
MsgBox "存在しません"
End If
End Sub
Sub Sample3()
Dim buf As String, i As Long, cnt As Long
buf = "ABC,DEF,GHI"
For i = 1 To Len(buf)
If Mid(buf, i, 1) = "," Then cnt = cnt + 1
Next i
MsgBox cnt & "個あります"
End Sub
Sub Sample3()
Dim buf As String, n As Long, cnt As Long
buf = "ABC,DEF,GHI"
Do
n = InStr(n + 1, buf, ",")
If n = 0 Then
Exit Do
Else
cnt = cnt + 1
End If
Loop
MsgBox cnt & "個あります"
End Sub
Sub Sample4()
Dim buf As String
buf = "田中 亨"
Select Case StrCount(buf, " ")
Case 0
MsgBox "半角スペースがありません"
Case 1
Debug.Print Split(buf, " ")(0)
Case Else
MsgBox "半角スペースが2つ以上あります"
End Select
End Sub
Function StrCount(Source As String, Target As String) As Long
Dim n As Long, cnt As Long
Do
n = InStr(n + 1, Source, Target)
If n = 0 Then
Exit Do
Else
cnt = cnt + 1
End If
Loop
StrCount = cnt
End Function