简体   繁体   中英

Excel VBA Count different Values from multiple Ranges

I want to find out different values ( text ) in 3 different ranges. I think I found a way online, but it gives me an error.

Here are the codes;

Dim MyDict As Object, MyCols As Variant, OutCol As String, LastRowXY_2 As Long
Dim XY_A As Variant, I_XY As Long, MyData_XY As Variant

Set MyDict = CreateObject("Scripting.Dictionary")
MyCols = Array("P", "S", "V")
OutCol = "AN"

For Each XY_A In MyCols
    LastRowXY_2 = Worksheets(Ders_Sheet_Adi).Cells(Rows.Count, XY_A).End(xlUp).Row
    MyData_XY = Worksheets(Ders_Sheet_Adi).Range(XY_A & "22:" & XY_A & LastRowXY_2).Value
    For I_XY = 1 To UBound(MyData_XY)
        If MyData_XY(I_XY, 1) <> "" Then MyDict(MyData_XY(I_XY, 1)) = 1
    Next I_XY
Next XY_A

Worksheets(Ders_Sheet_Adi).Range(OutCol & "1").Resize(MyDict.Count, 1).Value = WorksheetFunction.Transpose(MyDict.keys)

At the second run of "XY_A", MyData_XY gives a "Type-Mismatch" error.

I can use any kind of idea.

Option 1:

Change:

Worksheets(Ders_Sheet_Adi).Range(OutCol & "1").Resize(MyDict.Count, 1).Value = WorksheetFunction.Transpose(MyDict.keys)

to

Range(OutCol & "1").Resize(1, 1).Value = WorksheetFunction.Transpose(2)

and check whether it works now. If it does, you don't have any values in MyDict , thus, it throws an error.

Option 2: What do you think MyData_XY is? A range, an array? Try to call it like this -> MyData_XY(1)(1,1) instead of this -> MyData_XY(1,1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM