我是VBA的新手,我正在尝试根据列的内容对excel表进行分类。 我的第一个问题是 - 我有一个arraylist,我已经填充了数组,并且我无法访问arraylist中包含的数组的元素。(可以使用switch语句作为单词可能隐藏在文本中) 第二个问题是我必须初始化一个新数组而不是使用Object名称 - 我尝试过铸造。 非常感谢任何帮助。
'declare variable for the active cell for comparison
Dim ActiveTxt As String
Dim StringTxt As String
Dim Pop_Cell As Range
Dim msg As String
'intialize the Array list
Dim category_List As Object
Set category_List = CreateObject("System.Collections.ArrayList")
'Parent Array- has to match sequence of the intialised arrays
Dim Parent()
Parent = Array("Flights", "Accomodation", "Other_Subsistence")
'Array for Search terms
'**********************
'search terms are case sensitive
'**********************
Dim Flights()
Flights = Array("aerlin", "aerling", "ryanair", "ryan", "cityjet", "luft", "lufthansa", "aer", "transavia", "easyjet", "air", "swiss", "aero", "wow air"
Dim Accomodation()
Accomodation = Array("hotel")
Dim Other_Subsistence()
Other_Subsistence = Array("subsistance", "overnight")
'add Arrays to the arraylist
category_List.Add (Flights)
category_List.Add (Accomodation)
category_List.Add (Other_Subsistance)
'select first line of data
Range("A4").Select
'Set do loop to stop when an empty cell reached
Do Until IsEmpty(ActiveCell)
'to loop through the ArrayList (category_List)
For i = 0 To UBound(category_List(i))
'Loop through the Array
'For i = 0 To UBound(Flights)
'declaring variables for Search
ActiveTxt = ActiveCell.Text
'************************this is where the problem lies -used 1 as a test would use i and j and interate through them once it works
StringTxt = category_List.Item(1).get(1)
'Search by comparison- "if the cell contains this word"
If InStr(1, ActiveTxt, StringTxt) Then
'below makes a pop up box and populates it
'MsgBox ("found" & ActiveTxt)
'this populates the cell where the searched for value has been found with "flights" value
'*****this then needs to be Array
ActiveCell.Offset(0, 3).Value = StringTxt
'if found then exit the loop to stop searching though it
Exit For
Else
End If
Next i
Loop
答案 0 :(得分:1)
这些示例演示了如何引用和迭代存储在System.Collections.ArrayList
和Scripting Dictionary
中的数组。
注意:您只能从存储在集合中的数组中读取值。如果需要修改值,则需要将它们写入临时数组,更新临时数组,然后将它们重新分配给集合。
Sub ArrayListExample()
Dim Flights As Variant, Parent As Variant, Other_Subsistence As Variant
Dim list As Object
Set list = CreateObject("System.Collections.ArrayList")
list.Add Array("Flights", "Accomodation", "Other_Subsistence")
list.Add Array("aerlin", "aerling", "ryanair", "ryan", "cityjet", "luft", "lufthansa", "aer", "transavia", "easyjet", "air", "swiss", "aero", "wow air")
list.Add Array("subsistance", "overnight")
Parent = list.Item(0)
Flights = list.Item(1)
'This works because Item is the default property of an ArrayList
Other_Subsistence = list(2)
End Sub
Sub DictionaryExample()
Dim Flights As Variant, Parent As Variant, Other_Subsistence As Variant
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "Parent", Array("Flights", "Accomodation", "Other_Subsistence")
dict.Add "Flights", Array("aerlin", "aerling", "ryanair", "ryan", "cityjet", "luft", "lufthansa", "aer", "transavia", "easyjet", "air", "swiss", "aero", "wow air")
'This works because Item is the default property of an Scripting.Dictionary
dict("Other_Subsistence") = Array("subsistance", "overnight")
Parent = dict.Item("Parent")
Flights = dict.Item("Flights")
'This works because Item is the default property of an ArrayList
Other_Subsistence = dict("Other_Subsistence")
End Sub