简体   繁体   English

将单元格中的值范围转换为逗号分隔列表

[英]Turn a value range in a cell into a comma separated list

Is there a formula I can have in cell B1 that looks at A1 and creates a comma-based list? 在单元格B1中是否有一个公式可以查看A1并创建一个基于逗号的列表?

So below, A1 is something I can type into. 所以下面,A1是我可以输入的东西。 B1 is a formula. B1是一个公式。 Is this possible? 这可能吗? I will have A1 always follow the same format as a XXX-XXX range. 我将使A1始终遵循与XXX-XXX范围相同的格式。

+-------+-----------+----------------------+
| TABLE | A (Input) |      B (Result)      |
+-------+-----------+----------------------+
|     1 | 1-10      | 1,2,3,4,5,6,7,8,9,10 |
+-------+-----------+----------------------+

Put the code below in a regular VBA module then you can use (eg): 将下面的代码放在常规VBA模块中,然后您可以使用(例如):

=NumRange(A1)

in B1 在B1

Function NumRange(v)
    Dim arr, x As Long, rv As String, sep As String
    If InStr(v, "-") Then
        arr = Split(v, "-")
        arr(0) = Trim(arr(0))
        arr(1) = Trim(arr(1))
        If IsNumeric(arr(0)) And IsNumeric(arr(1)) Then
            For x = CLng(arr(0)) To CLng(arr(1))
                rv = rv & sep & x
                sep = ","
            Next x
        End If
    End If
    NumRange = rv
End Function

EDIT - handle multiple ranges 编辑 - 处理多个范围

Function NumRange(v)
    Dim arrC, arr, x As Long, rv As String, sep As String, e

    arrC = Split(v, ",")
    rv = ""

    For Each e In arrC
        If InStr(e, "-") Then
            arr = Split(e, "-")
            arr(0) = Trim(arr(0))
            arr(1) = Trim(arr(1))
            If IsNumeric(arr(0)) And IsNumeric(arr(1)) Then
                For x = CLng(arr(0)) To CLng(arr(1))
                    rv = rv & sep & x
                    sep = ","
                Next x
            End If
        ElseIf IsNumeric(e) Then
            rv = rv & sep & CLng(e)
            sep = ","
        End If
    Next e
    NumRange = rv
End Function

Another UDF approach 另一种UDF方法

=Spliced(A1) in B1 =Spliced(A1) B1 =Spliced(A1)

Function Spliced(strIn As String) As String
X = Split(strIn, "-")
Spliced = Join(Application.Transpose(Evaluate("=ROW(A" & X(0) & ":A" & X(1) & ")")), ",")
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据范围中的条件将逗号分隔值列表返回到单个Excel单元格 - How to Return a Comma Separated Value List to single Excel Cell based on Condition from Range Excel,将行单元格转换为逗号分隔的列表 - Excel, covert row cell into comma separated list Excel公式来检查一个单元格中的逗号分隔列表中的项是否存在于另一单元格中的逗号分隔列表中? - Excel formula to check if items from a comma separated list in one cell exist in a comma separated list in another cell? 从列的每个单元格中的逗号分隔列表中提取一个特定的键和值 - Extract one specific key and value from a comma separated list in every cell of a column Excel VBA 验证列表到单元格中的逗号分隔列表 - Excel VBA Validation List to Comma Separated List in Cell 确定并列出给定范围内的所有值,并在一个单元格中列出以逗号分隔的值 - Determine and list all values within a given range and list in one cell as a comma delimited value 以逗号分隔的列表显示Excel单元格值 - Displaying Excel cell values in a comma-separated list 根据单元格中的逗号分隔列表添加多个附件 - Add Multiple Attachments based on Comma Separated list in a cell 如何在以逗号分隔的单元格中创建字符串的可能组合列表 - How to create a list of possible combinations of strings in a cell separated by a comma 来自带有逗号分隔列表的单元格的Vlookup或表联接 - Vlookup or table join from a cell with a comma separated list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM