假設你的字符串數組是一維的,就像這樣工作?
Sub ArraySlicing()
Dim Xarray() As String
Dim Aarray() As String, Barray() As String, Carray() As String
Dim XA() As String, XB() As String, XC() As String
Dim XA_RwIndexList As String, XB_RwIndexList As String, XC_RwIndexlist As String
Dim i As Long
'create test data arrays
Xarray = Split("Red,Blue,Green,Yellow,Orange,Lime,Purple,Turquoise,Pink,Brown,White,Black,Gold", ",")
Aarray = Split("Apple,Orange,Banana,Lime,Pear,Orange,Green Apple,Red Apple", ",")
Barray = Split("Pink Rose,Tulip,Daisy,Bluebell,Carnation,Marigold", ",")
Carray = Split("redwood,spruce,lime,pine,oak,lemon,chestnut,walnut,orange", ",")
'loop XArray and check for matches
For i = LBound(Xarray) To UBound(Xarray)
'note application.match is case insensitive and will only match complete words
'i.e. "orange" & "Orange" will be found, "Pink Rose" will not
If Not IsError(Application.Match(Xarray(i), Aarray, 0)) Then _
XA_RwIndexList = XA_RwIndexList & "_" & i 'index matching rows
If Not IsError(Application.Match(Xarray(i), Barray, 0)) Then _
XB_RwIndexList = XB_RwIndexList & "_" & i 'index matching rows
If Not IsError(Application.Match(Xarray(i), Carray, 0)) Then _
XC_RwIndexlist = XC_RwIndexlist & "_" & i 'index matching rows
Next i
'check if XA_RwIndexList was initialized i.e. there were matches
If Not XA_RwIndexList = vbNullString Then
'trim preceeding "_" & store row #s in XA array
XA = Split(Mid(XA_RwIndexList, 2), "_")
'loop XA and replace row # with corresponding values from XArray
For i = LBound(XA) To UBound(XA)
XA(i) = Xarray(XA(i))
Next i
End If
'check if XB_RwIndexList was initialized i.e. there were matches
If Not XB_RwIndexList = vbNullString Then
'trim preceeding "_" & store row #s in XB array
XB = Split(Mid(XB_RwIndexList, 2), "_")
'loop XB and replace row # with corresponding values from XArray
For i = LBound(XB) To UBound(XB)
XB(i) = Xarray(XB(i))
Next i
End If
'check if XC_RwIndexList was initialized i.e. there were matches
If Not XC_RwIndexlist = vbNullString Then
'trim preceeding "_" & store row #s in XC array
XC = Split(Mid(XC_RwIndexlist, 2), "_")
'loop XC and replace row # with corresponding values from XArray
For i = LBound(XC) To UBound(XC)
XC(i) = Xarray(XC(i))
Next i
End If
End Sub
根據您的字符串匹配的要求Application.Match
未必是最合適的,但想到我會建議它,因爲它是避免需要也環路其他3個陣列的好方法。
我可以問你試圖解決你的問題嗎?如果你給我們一些東西來咀嚼,我們會更有幫助。 –
Romcel,我考慮通過Xarray循環並匹配每個主數組,但是希望更多的可以應用於集合或者矩陣或者數組的「ismember」。我在VBA上不流利,遇到試驗/錯誤,閱讀幫助,瀏覽網頁等問題。請注意,我不是程序員或IT人員,使用這種類型的代碼不是中心的到我的工作。只是在某些情況下,如果我編寫了一些自定義代碼,它會更容易處理一些重複的方面。我的頭腦現在混亂在不同的框架中可以做的事情。 – Jabberwocky