- 帖子
- 185
- 主題
- 48
- 精華
- 0
- 積分
- 227
- 點名
- 0
- 作業系統
- WIN 7
- 軟體版本
- 旗舰版
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2010-9-14
- 最後登錄
- 2025-1-27
|
2#
發表於 2024-1-28 00:41
| 只看該作者
- Option Explicit
- Sub 計算套裝級別()
- Dim ws As Worksheet
- Set ws = ThisWorkbook.ActiveSheet
-
- Dim lastRow As Long
- lastRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
-
- Dim i As Long
- Dim score As String
- Dim skip As Boolean
- Dim combinations As Variant
- Dim scores As Variant
-
- ' 定義套裝和級別
- combinations = Array("良優優良", "優良良優", "良優優優", "優良良良", "良優良", "優良優", "優優", "良良")
- scores = Array("2", "2", "3", "3", "3", "3", "3", "3")
-
- For i = 2 To lastRow
- If Not skip Then ' 如果skip為False,則執行此代碼塊
- Dim j As Long
- '檢查當前的字符序列(combo)是否與combinations數組中的任何一個元素匹配。
- '如果匹配,則根據對應的scores數組中的級別來更新score變量,並在工作表的"O"列中顯示該級別。
- For j = LBound(combinations) To UBound(combinations)
- Dim combo As String
- combo = ""
- On Error Resume Next
- combo = ws.Cells(i, "L").Value & ws.Cells(i + 1, "L").Value & ws.Cells(i + 2, "L").Value & ws.Cells(i + 3, "L").Value
- On Error GoTo 0
- '這段代碼的目的是在找到匹配的套裝時計算並顯示相應的級別。
- combo = Left(combo, Len(combinations(j))) ' 取組合的長度,這行代碼將combo字符串的左側部分(長度為combinations(j)的長度)賦值給combo。
- ' 這是為了確保combo的長度與當前的套裝combinations(j)的長度相同,以便進行比較。
- If combo = combinations(j) And ws.Cells(i, "L").Value <> ws.Cells(i - 1, "L").Value Then '檢查combo是否等於當前的套裝combinations(j),並且當前行的"L"列的值與前一行的"L"列的值不同
- score = scores(j)
- ws.Cells(i + Len(combinations(j)) - 1, "O").Value = score ' 在O列的相對行數顯示級別
- If score = 2 Or score = 3 Then
- skip = True ' 如果skip為True,則執行此代碼塊
- End If
- Exit For
- End If
- Next j
- Else
- '以下這段代碼的目的是在遇到新的字符序列開始時(即當前行的"L"列的值與前一行的"L"列的值不同,並且當前行的"L"列的值等於當前套裝的第一個字符時),停止跳過迭代。
- '如果skip為True,則會跳過當前的迭代,直到遇到新的字符序列開始。這樣可以防止在同一個字符序列中多次計算級別。
- If ws.Cells(i, "L").Value <> ws.Cells(i - 1, "L").Value And ws.Cells(i, "L").Value = Left(combinations(j), 1) Then
- skip = False
- End If
- End If
- Next i
- End Sub
複製代碼 |
|