Excel中的自定义文本,使用宏表示两个数字之间的值

问题描述 投票:0回答:2

我有一列数据来自1-100,我想将其分类为4个区域

Zone A : 0 < val <= 20
Zone B : 20 < val <= 40
Zone C : 40 < val <= 60
Zone D : 60 < val <= 100

如何使用xls中的宏将A,B,C和D区域放在其前面?

excel vba excel-vba xls
2个回答
0
投票

您可以做类似的事情

Function zone_num(intNum As Integer)

zone_num = IIf(intNum < 0, "Error -ve", _
            IIf(intNum < 20, "Zone A", _
            IIf(intNum < 40, "Zone B", _
            IIf(intNum < 60, "Zone C", _
            IIf(intNum < 100, "Zone D", _
            "Error too high")))))

End Function

0
投票

在这种情况下,有多种前往罗马的方式,例如INDEXMATCH


样本数据:] >>

enter image description here


代码:

Sub Test()

Dim lr As Long, x As Long, arr As Variant

With Sheet1'Change according to your sheets CodeName

    'Find last used row in column and fill array
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    arr = .Range("A1:A" & lr)

    'Go through array and repopulate it
    With Application
        For x = LBound(arr) To UBound(arr)
            arr(x, 1) = .Index(Array("A", "B", "C", "D"), .Match(arr(x, 1), Array(0, 21, 41, 61))) & arr(x, 1)
        Next x
    End With

    'Write new array to range
    .Range("A1:A" & lr) = arr

End With

End Sub

[结果:

]

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.