在一些其他细胞中重复细胞

时间:2010-06-23 13:34:26

标签: excel excel-2007

我有一个excel填充这些数据:

Name1
1
2
3
Name2
1
2
3
4
Name3
1
2
......

我希望将名称放在它下面每行前面的单元格中,如下所示:

1          Name1
2          Name1
3          Name1
1          Name2
2          Name2
3          Name2
4          Name2
1          Name3
2          Name3
....

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

以下是解决此问题的一种方法;它会做出一些假设,如果结果是假的,你可能需要解决这些假设。

Dim n As String
Dim v As String

Dim row As Long
Dim col As Integer

Dim c As Range

Dim destRow As Long
Dim destCol As Integer

'Assume your data is in column A, starting in row 1
row = 1
col = 1

'and that you want to drop it into columns C and D starting in row 1
destRow = 1
destCol = 3

Do
    Set c = ActiveSheet.Cells(row, col)
    If Left(c.Value, 4) = "Name" Then
        n = c.Value
    ElseIf n <> "" Then
        Cells(destRow, destCol).Value = c.Value
        Cells(destRow, destCol + 1).Value = n
        destRow = destRow + 1
    End If
    row = row + 1
Loop Until ActiveSheet.Cells(row, col).Value = ""