VBA从字符串解析数值范围

时间:2013-09-07 18:57:10

标签: parsing vba excel-vba excel

我需要解析用户表单(VBA,Excel)中的数值范围。 例如:{1-3,5,9} - > {1,2,3,5,9,}

这里已经讨论过这个问题了: Advanced parsing of numeric ranges from string

这项任务非常标准,是否有针对VBA的现成解决方案?

1 个答案:

答案 0 :(得分:0)

我假设包围的大括号不是字符串的一部分。请考虑以下UDF:

Public Function DashFiller(sIn As String) As String
    Dim N As Long, NN As Long
    DashFiller = ""
    ary = Split(sIn, ",")
    For N = LBound(ary) To UBound(ary)
        If InStr(1, ary(N), "-") > 0 Then
            ary2 = Split(ary(N), "-")
            ary(N) = ary2(0)
            For NN = ary2(0) + 1 To ary2(1)
                ary(N) = ary(N) & "," & NN
            Next NN
        End If
    Next N
    DashFiller = Join(ary, ",")
End Function

所以如果A1包含: 1-2,3,4,5,9-12

和B1包含: = DashFiller(A1)

然后B1会显示: 1,2,3,4,5,9,10,11,12