结合多重macros

我试图制作一个macros来计算列中列出的城市/可能城市的数量,但是由于我在第一个数组中创build了一个有限的字符(正是这样工作的)。 然后我添加了Dim Cities2()与第二个数组,但得到错误“types不匹配错误”。 请帮忙。 顺便说一句,我仍然需要添加约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 

你可以尝试不同的方法。

创build一个Dictionary对象并将这些城市添加为Keys。 字典的好处是你可以检查一个Key(城市)是否存在。

如果城市存在,增加价值(柜台)。 如果不是,则添加值为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 

样本数据:

一个

一个

C

C

C

d

Ë

F

F

G

H

[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 

对你的计数器使用单字母或字母数字组合将增加你的代码的可读性。 每当你看到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循环中使用的variables是惯例。 这是一个典型的模式:

注意:根据情况,variables必须是Variant或Objecttypes。 变体types始终有效。 您可以在1D或multidimensional array上使用此循环。 该循环遍历数组的每个元素。 在完全遍历第一列(维)的元素之后,它从下一列(维)的开始处开始。

 Dim city As Variant For Each city In Cities Debug.Print city Next