Excel - 根据换行符向单元格文本添加序号

时间:2016-08-23 14:01:24

标签: excel vba excel-vba

我有一个包含项目信息的工作表。该工作表包含一个包含每个项目的风险的列。项目与风险之间存在一对多的关系。

目前,项目的风险被添加到单个单元格中,并以换行符分隔。我需要在每个风险开始时添加顺序标识符。因此,例如在特定单元格内,它应该看起来像这样。如果可能的话,序号应该是粗体。

1)。**风险1

2)。**风险2

3)。**风险3

任何有关如何解决此问题的建议都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

以下是我通过UDF处理它的方法:

' Reformats a list from a simple delimitation to a numbered list
' Accepts arrays of strings for inList (allowing array formulas)
'  numFormat is a standard Excel-style format string (default "0. ")
'  inDelimiter is the delimiter in the input list
'  outDelimiter is the delimiter for the output list
Public Function TO_NUMBERED_LIST(inList As Variant, Optional numFormat As Variant, _
 Optional inDelimiter As Variant, Optional outDelimiter As Variant) As Variant
    Dim i As Integer, j As Integer

    ' Set default parameters
    If IsMissing(numFormat) Then numFormat = "0). "
    If IsMissing(inDelimiter) Then inDelimiter = vbNewLine
    If IsMissing(outDelimiter) Then outDelimiter = inDelimiter

    If IsArray(inList) Then ' Must loop through each entry if using as an array formula
        Dim outList() As Variant
        ReDim outList(0 To (UBound(inList) - LBound(inList)), 1 To 1)

        j = 0
        For i = LBound(inList) To UBound(inList)
            If IsError(inList(i, 1)) Then
                outList(j, 1) = inList(i, 1)
            Else
                outList(j, 1) = MakeNumbered(CStr(inList(i, 1)), CStr(numFormat), CStr(inDelimiter), CStr(outDelimiter))
            End If
            j = j + 1
        Next

        TO_NUMBERED_LIST = outList
    Else
        TO_NUMBERED_LIST = MakeNumbered(CStr(inList), CStr(numFormat), CStr(inDelimiter), CStr(outDelimiter))
    End If
End Function

' Helper function to do the actual work of splitting lists, numbering them, and recombining them
Private Function MakeNumbered(inList As String, Optional numFormat As String, _
 Optional inDelimiter As String, Optional outDelimiter As String) As String
    Dim i As Integer
    Dim tokenArr() As String
    tokenArr = Split(inList, inDelimiter)

    For i = 0 To UBound(tokenArr)
        tokenArr(i) = Format(i + 1, numFormat) & tokenArr(i)
    Next

    MakeNumbered = Join(tokenArr, outDelimiter)
End Function

我利用你之前的线程中的一些知识,比如输入可能是一个数组(并且整个函数可能在数组公式中使用),但只会是一维的。

我为重新格式化做了很多。它可以包含任何输入分隔符的列表(在您的情况下,换行符)并使用任何所需的分隔符输出(在您的情况下,仍然是换行符)。 numFormat参数使用Format函数,并支持您在Excel中常见的格式。如果您需要帮助,请查看the documentation

默认参数已经针对您的示例进行了调整 - 换行符作为分隔符,“0”。“作为编号格式。

答案 1 :(得分:0)

您可以在每个单元格值上使用Split函数来创建一系列风险,然后使用序列ID为每个风险添加前缀。然后,您可以使用Join函数将数组放回单个值以更新单元格。

根据新行进入单元格的方式,您可能需要在以下示例代码中使用vbCrLfvbNewLine代替vbLf

Option Explicit

Sub AddRiskSequence()

    Dim rngRisks As Range
    Dim rngCell As Range
    Dim varRisks As Variant
    Dim lngIndex As Long

    'set range with risk values
    Set rngRisks = Sheet2.Range("B2:B4")

    'iterate cells in risk column
    For Each rngCell In rngRisks
        'split cell contents by line feed into array
        varRisks = VBA.Split(rngCell.Value, vbLf)
        'iterate array and add sequence ids
        For lngIndex = 0 To UBound(varRisks)
            varRisks(lngIndex) = VBA.CStr(lngIndex + 1) & ") " & varRisks(lngIndex)
        Next lngIndex
        'rejoin array and update cell value
        rngCell.Value = VBA.Join(varRisks, vbLf)
    Next rngCell

End Sub

在:

enter image description here

后:

enter image description here