返回列表 上一主題 發帖

處理STRING時遇到困難

本帖最後由 GBKEE 於 2012-8-24 12:07 編輯

回復 3# 小俠客
  1. Option Explicit
  2. Sub Ex()
  3.     Dim TheWord As String, xW As String, E
  4.     xW = "a"
  5.     TheWord = "我aaaaa是aaaaa一個aaaaaaaaa男生"
  6.     Do
  7.         TheWord = Replace(TheWord, String(2, xW), xW)
  8.     Loop While InStr(TheWord, String(2, xW))  '= True
  9.     For Each E In Split(TheWord, xW)
  10.         MsgBox E
  11.     Next
  12. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2012-8-28 09:15 編輯

回復 7# 小俠客
  1. If InStr(raw.Cells(i, 3), "小學") + InStr(raw.Cells(i, 3), "初小") > 0 Then  
  2.         '條件 : 如不成立, 會判斷下一個ElseIf條件.一直到有ElseIf條件成立後會不再去判斷其餘的條件,離開此 IF ....Then  ....Else  .....     End If
  3.         '不必   GoTo Define_unit
  4.         vbtype = "primary"                                                                                                            
  5.     ElseIf InStr(raw.Cells(i, 3), "初中") + InStr(raw.Cells(i, 3), "國中") + InStr(raw.Cells(i, 3), "高中") > 0 Then
  6.         vbtype = "secondary"
  7.     ElseIf InStr(raw.Cells(i, 3), "大學") + InStr(raw.Cells(i, 3), "大專") > 0 Then
  8.         vbtype = "Uni"
  9.     ElseIf InStr(raw.Cells(i, 3), "研究所") + InStr(raw.Cells(i, 3), "博士") > 0 Then
  10.         vbtype = "Master+"
  11.     End If
  12.     If InStr((raw.Cells(i, 3), "台灣") > 0 Then unit = "TW"
  13.     If InStr((raw.Cells(i, 3), "香港") > 0 Then unit = "HK"
  14.     If InStr((raw.Cells(i, 3), "美國") > 0 Then unit = "US"
複製代碼

TOP

本帖最後由 GBKEE 於 2012-8-28 09:48 編輯

回復 9# 小俠客
InStr(raw.Cells(i, 3), "小學")   這只會傳回數字
你自串中 初小, 小學,初中, 國中.... 並沒有固定在哪個位置,不適合用 Select Case
  1. Select Case 變數  '
  2.       Case  數字 或 字串
  3. End Select
複製代碼

TOP

本帖最後由 GBKEE 於 2012-8-29 13:09 編輯

回復 11# 小俠客
  1. Option Explicit
  2. Public scl, edu                  '此公用變數 可供其他程序使用   
  3. Private Sub Workbook_Open()  ' Workbook_Open 是ThisWorkbook 模組的程式碼
  4.     設立陣列
  5. End Sub
  6. Sub 設立陣列()  '在此設立陣列 可供其他程序使用   
  7.     ReDim scl(1 To 3, 1 To 2)
  8.     ReDim edu(1 To 3)
  9.     scl(1, 1) = "初小"
  10.     scl(1, 2) = "小學"
  11.     scl(2, 1) = "初中"
  12.     scl(2, 2) = "國中"
  13.     scl(3, 1) = "大學"
  14.     scl(3, 2) = "專科"
  15.     edu(1) = "小學"
  16.     edu(2) = "中學"
  17.     edu(3) = "大學"
  18. End Sub
  19. Sub Ex()   '執行 "設立陣列" 程序後可執行此程序
  20.     Dim j, k, w, vbtype
  21.     w = "--國中**"
  22.     For j = 1 To UBound(scl)
  23.         For k = 1 To UBound(scl, 2)
  24.            If InStr(w, scl(j, k)) Then vbtype = edu(j): GoTo Define_unit
  25.         Next k
  26.     Next j
  27. Define_unit:
  28.     MsgBox vbtype
  29. End Sub
複製代碼

TOP

        靜思自在 : 原諒別人就是善待自己。
返回列表 上一主題