如何在两个给定编号之间生成列表。在Excel中

时间:2018-06-19 14:48:14

标签: excel list between generate

请帮助您生成带有给定两个no的一系列列表的技巧。

例如:

具有这样的数据:

Key      | Month | Year | Location | From | To    
HYE000001| 12    |2013  | 91       | 01   | 52

,并希望使用以下给出的格式输出:

Key      | Month | Year | Location
HYE000001| 12    | 2013 | 91
HYE000002| 12    | 2013 | 91
HYE000003| 12    | 2013 | 91
HYE000004| 12    | 2013 | 91
.
.
.
HYE000051| 12    | 2013 | 91
HYE000052| 12    | 2013 | 91

请帮助我解决问题

谢谢。

  • Shiven

1 个答案:

答案 0 :(得分:0)

在这里,我尝试使用宏并为我工作良好。下面的代码段仅适用于键值,您可以根据需要进行修改。

将Key值放在A2单元格中,将From值放在B2单元格中,并将To值放在C2单元格中,然后尝试运行以下代码。

 Sub KeyGenerator()

    'From range defined in B2 Cell
    Dim From As Integer
    From = Range("B2").Value

    'To range defined in C2 Cell
    Dim Till As Integer
    Till = Range("C2").Value

    'Here A2 cell will have the primary key and modifying the A2 cell value using From value.
    Dim LastKey As String
    Dim Key, Current As String
    Current = Range("A2").Value
    Key = Left(Current, Len(Current) - 1)
    Key = Key & From
    Range("A2").Value = Key

    'Dragging values using To value
    Range("A2").Select
    LastKey = "A" & ((Till - From) + 2)
    Selection.AutoFill Destination:=Range("A2:" & LastKey), Type:=xlFillDefault

    End Sub