您正在尋找的功能是Split(此鏈接適用於VB函數,但在VBA中的行爲幾乎相同)。您可以將特定的字符串傳遞給它並指定分隔符,並且它將返回每個值的數組。
就你而言,由於你的字符串有多個分隔符,你需要多次執行。
第一個潛在的問題是如果您沒有給定類別的子類別?如果您的字符串中的每個類別都至少包含子類別,那麼很好,但如果有潛在的情況下沒有子類別,那麼您需要確保您的最高級別分組仍然由;
分隔。
由於您沒有說明如何將信息呈現給用戶,因此下面的示例將打印出與您在Excel中的中間窗口中期望的內容相近的內容。
Option Explicit
Sub SplitExample()
Dim inputString As String
Dim categories() As String
Dim subCategories() As String
Dim individualSubCat() As String
Dim cat As Variant
Dim subCat As Variant
Dim cnt As Integer
inputString = "1:1i;2:2a;3:3a,3d,3l;4:4a"
categories = Split(inputString, ";")
For Each cat In categories
subCategories = Split(cat, ":")
If UBound(subCategories) >= 0 Then
Debug.Print ("Category " & subCategories(0))
If UBound(subCategories) > 0 Then
individualSubCat = Split(subCategories(1), ",")
Debug.Print (vbTab & "Has " & UBound(individualSubCat) - LBound(individualSubCat) + 1 & " flag(s)")
For Each subCat In individualSubCat
Debug.Print (vbTab & subCat & " was flagged " & CountSubCategory(individualSubCat, subCat) & " time(s)")
Next
Else
Debug.Print (vbTab & "No Subcategories flagged")
End If
Debug.Print ("")
End If
Erase subCategories
Erase individualSubCat
Next
End Sub
這是將計算的子類別很容易
Private Function CountSubCategory(individualSubCategories() As String, ByVal subCat As String) As Integer
Dim cnt As Integer
Dim value As Variant
cnt = 0
For Each value In individualSubCategories
If value = subCat Then cnt = cnt + 1
Next
CountSubCategory = cnt
End Function
和使用例如字符串作爲輸入的功能,上面的代碼將打印:
Category 1
Has 1 flag(s)
1i was flagged 1 time(s)
Category 2
Has 1 flag(s)
2a was flagged 1 time(s)
Category 3
Has 3 flag(s)
3a was flagged 1 time(s)
3d was flagged 1 time(s)
3l was flagged 1 time(s)
Category 4
Has 1 flag(s)
4a was flagged 1 time(s)
將上面的代碼打印每個標誌,即使有重複。你沒有說這是否是期望的行爲。從數組中過濾或分組重複並不簡單,但最好使用VBA中的Collection或Dictionary類完成。 (請看this question以幫助過濾數組中的重複項)。
上面的代碼只是一個示例,顯示需要完成什麼以及如何完成解析(因爲這是您的特定請求)。要真正使之成爲工作代碼這一點,你需要做兩件事情:
- 創建VBA函數或子的代碼,以上述分析它(基本上裏面有什麼東西上面
SplitExample()
),並給它一個名稱(如ParseErrorCodes
),並讓它接受名爲inputString
的字符串參數。然後你可以從構建字符串的方法(你說你已經可以做到)調用它並將該字符串傳遞給方法。
- 決定如何輸出結果。實際上,你會用行替換某些地方的結果(可能是另一個excel電子表格,因此你可以創建你正在查找的圖表)。
的基本思路是:
Sub OutputErrorCodes()
Dim inputString as String
' You code to read your string values from where-ever you keep them
' and build the inputString
' this source could be a file, or a worksheet in the Excel Workbook
' or could be an external datasource like a database or even an internet
' location
' once you build your inputString, you just need to call ParseErrorCodes
ParseErrorCodes inputString
End Sub
Sub ParseErrorCodes(input as String)
' MyCode from above with some modifications
' - You need to remove the Dim for inputString and the assignment for
' inputString
' - You need to replace the Debug.Print lines with your method of
' defining the output
' * this can be via outputing directly to an excel spreadsheet or
' maybe a global variable
' * outputing to an excel spreadsheet would probably be the best
' option and allow for more dynamic flags, but you need to decide
' where to output it in the code
End Sub
Private Function CountSubCategory(individualSubCategories() As String,
ByVal subCat As String) As Integer)
' this code can probably be taken directly from my example
End Function
謝謝你的迴應,這是非常有幫助的。一個問題:上面的代碼如何被送入VBA?理想情況下,有一張帶有多行代碼的列表。我需要拉出所有的代碼,並將它們轉化爲趨勢分析的根本原因,然後繪製最高缺陷區域(3a或3l)等。 – user1005237
在我的示例中,我剛剛使用示例字符串定義了一個字符串:'inputString = 「1:1I; 2:2A; 3:3A,3D,3L; 4:4A」'。您必須從另一個讀取輸入(可能是Excel電子表格)的方法中爲其提供特定的字符串 – psubsee2003
@ user1005237我在一些更基本的用法詳細信息中添加了我的答案。但是,大多數情況下,假定您對Excel中的VBA有基本的瞭解,以及如何調用方法以及從Excel電子表格和其他基本I/O讀取/寫入數據。 – psubsee2003