Excel。将文本字符串和重复次数复制到新列中

时间:2018-10-04 21:55:20

标签: vba excel-formula

给出两列(A和B),一列带有文本,一列带有整数,例如:

A   |   B
pen |   3
pen |   5

我如何在C,D,E [...]列中用给定字符串的连接填充每一行,并且所有整数从1开始到指定的数字?

给定示例的所需输出为:

A    |  B  |  C      |  D      |  E      |  F      |  G
pen  |  3  |  pen01  |  pen02  |  pen03  |         |
pen  |  5  |  pen01  |  pen02  |  pen03  |  pen04  |  pen05

2 个答案:

答案 0 :(得分:2)

使用简单的vba sub可以实现:

Sub CreateValues
    With ActiveSheet
        Dim LastRow as Long: LastRow = .Range("A" & .Rows.Count).End(xlup).Row
        For i = 1 To LastRow
            Max_Num = .Cells(i, 2)
            For j = 1 to Max_Num
                .Cells(i, j + 2) = .Cells(i, 1) & Format(j, "00")
            Next j
        Next i
    End With
End Sub

答案 1 :(得分:2)

如果您要寻找公式解决方案而无需求助于VBA,则可以在C1中使用此公式并在两个维度中拖动:

=IF(COLUMNS($C1:C1)<=$B1,CONCATENATE($A1,TEXT(COLUMNS($C1:C1),"00")),"")