Excel VBA:满足某些条件时的动态数组

Excel VBA:满足某些条件时的动态数组,excel,vba,Excel,Vba,这是我的问题 我的excel中有这样的数据 Manufactuer Model Capacity x 1000 x 3000 y 1800 y 4000 z 1300 我想循环浏览这些数据,当制造商相同时,我想获得最小容量并

这是我的问题

我的excel中有这样的数据

Manufactuer     Model Capacity

x                   1000                    
x                   3000   
y                   1800                 
y                   4000 
z                   1300 
我想循环浏览这些数据,当制造商相同时,我想获得最小容量并将其复制到另一张图纸上。问题是我不知道每个制造商的确切型号数量。例如,一次可以找到5次X Manufacturer,另一次可以找到4次。

如果制造商列为A:A,型号容量列为B:B,则公式

{=MINIFA:A=y;B:B}

将为您提供y制造商1800的最小值

更改字母y以更改制造商,然后按Ctrl+Shift+Enter键放置大括号。

如果制造商列为A:A,模型容量列为B:B,则公式

{=MINIFA:A=y;B:B}

将为您提供y制造商1800的最小值


更改字母y以更改制造商,然后按Ctrl+Shift+Enter组合键放置大括号。

修改后尝试此操作,以适合您自己的范围和工作表名称

sub minCapacities()

    dim i as long, arr as variant, dict as object

    set dict = createobject("scripting.dictionary")
    dict.comparemode = vbtextcompare

    with worksheets("sheet1")
        arr = .range(.cells(2, "A"), .cells(.rows.count, "B").end(xlup)).value2
    end with

    for i=lbound(arr, 1) to ubound(arr, 1)
        if dict.exists(arr(i, 1)) then
            dict.item(arr(i, 1)) = application.min(dict.item(arr(i, 1)), arr(i, 2))
        else
            dict.item(arr(i, 1)) = arr(i, 2)
        end if
    next i

    with worksheets("sheet2")
        .cells(1, "A").resize(1, 2) = array("Manufactuer", "Min Capacity")
        .cells(2, "A").resize(dict.count, 1) = application.transpose(dict.keys)
        .cells(2, "B").resize(dict.count, 1) = application.transpose(dict.items)
    end with

end sub

修改后尝试此操作,以适合您自己的范围和工作表名称

sub minCapacities()

    dim i as long, arr as variant, dict as object

    set dict = createobject("scripting.dictionary")
    dict.comparemode = vbtextcompare

    with worksheets("sheet1")
        arr = .range(.cells(2, "A"), .cells(.rows.count, "B").end(xlup)).value2
    end with

    for i=lbound(arr, 1) to ubound(arr, 1)
        if dict.exists(arr(i, 1)) then
            dict.item(arr(i, 1)) = application.min(dict.item(arr(i, 1)), arr(i, 2))
        else
            dict.item(arr(i, 1)) = arr(i, 2)
        end if
    next i

    with worksheets("sheet2")
        .cells(1, "A").resize(1, 2) = array("Manufactuer", "Min Capacity")
        .cells(2, "A").resize(dict.count, 1) = application.transpose(dict.keys)
        .cells(2, "B").resize(dict.count, 1) = application.transpose(dict.items)
    end with

end sub

MINIFS或伪MINIF有什么问题?可以使用透视表或Power Query。MINIFS或伪MINIF有什么问题?可以使用透视表或Power Query。