- 帖子
- 522
- 主題
- 36
- 精華
- 1
- 積分
- 603
- 點名
- 0
- 作業系統
- win xp sp3
- 軟體版本
- Office 2003
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2012-12-13
- 最後登錄
- 2021-7-11
|
2#
發表於 2014-4-1 19:46
| 只看該作者
本帖最後由 yen956 於 2014-4-1 19:48 編輯
試試看:- Option Base 1
- Option Explicit
- Private Sub CommandButton1_Click()
- Dim sh1, sh3 As Worksheet
- Dim Rng, cel As Range
- Dim strR, strL As String
- Dim i, j, r, blankCol, cnt(1 To 10), cnt2(1 To 10) As Integer
- Dim num1, num2
- num1 = Array("1", "2", "3", "4", "5", "6", "7", "8", "9")
- num2 = Array("一", "二", "三", "四", "五", "六", "七", "八", "九")
-
- Set sh1 = Sheets("輸入資料")
- Set sh3 = Sheets("分類結果")
-
- '清除 sh3 的資料
- sh3.[C3].Resize(41, 200) = ""
-
- '以你的示範檔來說, 輸入區從 [A4] 開始算, 共 14列, 9 欄
- '請自行依 實際輸入範圍 修改 Resize(14, 9) 的數據
- Set Rng = sh1.[A4].Resize(14, 9)
-
- For i = 1 To 10
- cnt(i) = 0
- cnt2(i) = 0
- Next
-
- For Each cel In Rng
-
- '若 cel 是空值, 換下一個
- If cel.Value = "" Then GoTo next1:
-
- '取得最右一碼
- strR = Right(cel, 1)
-
- '去除最右一碼
- strL = Left(cel, Len(cel) - 1)
-
- '比對 strR 在 num1 中 排第幾個
- j = Application.Match(strR, num1, 0)
-
- '若 match 產生錯誤 → 表示 strR 不在陣列 num1 中,
- If Application.IsError(j) Then
-
- '則繼續比對 strR 在 num2 中 排第幾個
- j = Application.Match(strR, num2, 0)
-
- '若 match 產生錯誤 → 表示 strR 也不在陣列 num2 中,
- If Application.IsError(j) Then
-
- 'strR 均不在 num1、num2 中,
- '→ 表示 strR 應歸類在 unclassified 中,
- '統計 unclassified 的總數
- cnt(10) = cnt(10) + 1
-
- '取得 空白儲存格 的 欄值
- blankCol = sh3.Cells(cnt2(10) + 40, 256).End(xlToLeft).Column + 1
- If blankCol < 3 Then blankCol = 3
-
- sh3.Cells(cnt2(10) + 40, blankCol) = cel
-
- '重新計算存入 unclassified 類的列值
- cnt2(10) = cnt2(10) + 1
- If cnt2(10) >= 3 Then cnt2(10) = 0
-
- Else
- '統計各組的總數
- cnt(j) = cnt(j) + 1
-
-
- '取得 空白儲存格 的 欄值
- blankCol = sh3.Cells(j * 4 + cnt2(j), 256).End(xlToLeft).Column + 1
- If blankCol < 3 Then blankCol = 3
-
- sh3.Cells(j * 4 + cnt2(j), blankCol) = strL
-
- '重新計算存入 各組 的列值
- cnt2(j) = cnt2(j) + 1
- If cnt2(j) >= 3 Then cnt2(j) = 0
-
- End If
- Else
- '統計各組的總數
- cnt(j) = cnt(j) + 1
-
- '取得 空白儲存格 的 欄值
- blankCol = sh3.Cells(j * 4 + cnt2(j), 256).End(xlToLeft).Column + 1
- If blankCol < 3 Then blankCol = 3
-
- sh3.Cells(j * 4 + cnt2(j), blankCol) = strL
-
- '重新計算存入 各組 的列值
- cnt2(j) = cnt2(j) + 1
- If cnt2(j) >= 3 Then cnt2(j) = 0
- End If
-
- next1:
- Next
-
- '填入各組總數
- For i = 1 To 10
- sh3.Cells(i * 4 - 1, 3) = cnt(i)
- Next
- End Sub
複製代碼 如下圖:
 |
|