我正在尝试创建一个宏来计算一列中列出的城市/可能城市的数量,但因为我在我创建的第一个数组中写了一个有限的字符。(它正是这样工作的) 。 然后我添加Dim Cities2()与第二个数组,但得到错误“类型不匹配错误”。请帮忙。顺便说一句,我仍然需要在阵列列表中添加大约200个城市,但还没有添加它们。谢谢你的帮助。
Public Sub CountLocation1()
Dim wb As Workbook
Dim ws As Worksheet
Dim lastCell As String
Dim countRange As Range
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet 'Change as appropriate
Set countRange = ws.Range(Cells(2, "V"), Cells(ws.Range("V2").End(xlDown).Row, "V"))
Debug.Print countRange.Address
Dim Cities()
Cities = Array("Auckland", "Brisbane", "Melbourne", "Seoul", "Tokyo", "Sydney", "Bratislava", "Bangalore", "Chennai", "Gurgaon", "Hyderabad", "Kolkata", "New Delhi", "Noida", "Mumbai", "London", "Munich", "Unterfohring", "Aachen", "Abidjan", "Abington", "Alpharetta", "Amstelveen", "Amsterdam", "Anaheim", "Aquascalientes", "Arlon", "Ashland", "Atlanta", "Aurora", "Austin", "Barcelona", "Basel", "Batavia", "Bay Village", "Belton", "Berkshire", "Berlin", "Birmingham", "Bogota", "Boise", "Boston", "Bramley", "Brandon", "Brecksville", "Brentwood", "Bridgetown", "Brussels", "Budapest", "Buffalo Grove", "Bury", "Cairo", "Callahan", "Calumet City", "Cape Town", "Capitola", "Cardiff", "Carmel", "Centennial", "Chanhassen", "Charlotte", "Cheltenham", "Cincinnati", "Clearwater", "Clemson", "Cleveland", "Cohoes", "Columbia", "Columbus", "Conifer", "Cookeville", "Copenhagen", "Coral Gables", "Croydon", "Culver City", "Cumming", "Cutchogue", "Dallas", "Dallas Park", "Darmstadt", "Double Oak", "Dublin")
Dim Cities2()
Cities2 = Array("Eagle Rock", "East Hartford", "Elk Grove", "Encino", "Enfield", "Erfurt", "Eschborn", "Euless", "Fairfield", "Fenton", "Folkestone", "Folsom", "Frankfurt", "Franklin", "Frisco", "Garden City", "Geneva", "Germantown", "Glendale", "Glenview", "Gloucester", "Greensboro", "Greenwood", "Greenwood Village", "Grove", "Hamburg", "Hamilton", "Harrisonville", "Hartford", "Hatfield", "Hiawatha", "Hitchin", "Hofstetten", "Homewood", "Hope", "Houston", "Hudson", "Illinois", "Indianapolis", "Itasca", "Jackson", "Jacksonville", "Jaipur", "Johannesburg", "Jordbro", "Katy", "Kirkwood", "Ladera Ranch", "Lake Forest", "Lakewood", "Lancaster", "Largo", "Lawrenceville", "Leawood", "Lexington", "Liberty", "Lincoln", "Lockport", "Lombard", "Luxembourg", "Lörrach", "Madrid", "Manchester", "Maple Glen", "Martins Ferry", "Marupe", "Masontown", "Matthews", "McKinney", "Mechanicsville", "Middletown", "Milan")
Dim city As Long
Dim counter As Long
Dim startRange As Range
Set startRange = ws.Cells(ws.Range("V2").End(xlDown).Row, "V").Offset(2, 0)
counter = 2
For city = LBound(Cities, Cities2) To UBound(Cities, Cities2)
If Application.WorksheetFunction.CountIf(countRange, Cities(city)) > 0 Then
startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities, Cities2(city))
startRange.Offset(counter, 1) = Cities & Cities2(city)
counter = counter + 1
End If
Next city
End Sub
答案 0 :(得分:1)
您可以尝试不同的方法。
创建一个Dictionary
对象,并将城市添加为密钥。关于字典的好处是你可以检查Key(city)是否存在。
如果城市存在,请递增值(计数器)。如果没有,请添加值为1。
下面的示例添加了位于A列的列表。修改它以满足您的需求。
Sub GetCityCount()
Dim ws As Worksheet
Dim objDict As Object
Set ws = ThisWorkbook.ActiveSheet
Set objDict = CreateObject("Scripting.Dictionary")
Dim lngCount As Long
lngCount = ws.Cells(Rows.Count, "A").End(xlUp).Row 'Column A
Dim idx As Long
For idx = 1 To lngCount
If Not objDict.Exists(ws.Cells(idx, 1).Value) Then
'Add to dictionary with a count of 1
objDict.Item(ws.Cells(idx, 1).Value) = 1
Else
'Increment counter
objDict.Item(ws.Cells(idx, 1).Value) = objDict.Item(ws.Cells(idx, 1).Value) + 1
End If
Next
'Test
Dim k As Variant
For Each k In objDict.Keys
Debug.Print "Key: " & k & ", Count: " & objDict(k)
Next k
End Sub
示例数据:
A
A
乙
乙
C
C
C
d
电子
˚F
˚F
ħ
[R
Ť
Ť
Ť
输出:
'Key: A, Count: 2
'Key: B, Count: 2
'Key: C, Count: 3
'Key: D, Count: 1
'Key: E, Count: 1
'Key: F, Count: 2
'Key: G, Count: 1
'Key: H, Count: 1
'Key: R, Count: 1
'Key: T, Count: 3
答案 1 :(得分:0)
对您的计数器使用单个字母或字母数字组合将增加代码的可读性。每当你看到i, j, x, y, i1, i2
......等等。你应该知道它是一个柜台。
LBound和UBound的第一个参数是一个数组,第二个参数是你要定位的维度。
以下是用于迭代数组的典型For
循环:
For x = LBound(Cities) To UBound(Cities)
Debug.Print Cities(x) '1D Array
Next
For x = LBound(Cities) To UBound(Cities)
Debug.Print Cities(x, 2) 'The 2 column of a 2D Base 1 array
Next
For x = LBound(Cities, 2) To UBound(Cities, 2)
'2nd column of a 2D Base 1 array
Next
通常的做法是使用名词作为For Each
循环中使用的变量。这是一个典型的模式:
注意:变量必须是Variant或Object类型,具体取决于具体情况。变体类型始终有效。您可以在1D或多维数组上使用此循环。该循环遍历数组的每个元素。在完全迭代第一列(维度)的元素之后,它从下一列(维度)的开头开始。
Dim city As Variant
For Each city In Cities
Debug.Print city
Next