Excel VBA文本到列

时间:2010-05-12 00:47:28

标签: excel-vba vba excel

这就是我目前所拥有的:

H101    John Doe    Jane Doe    Jack Doe    
H102    John Smith  Jane Smith  Katie Smith Jack Smith

这就是我想要的:

H101    John Doe
H101    Jane Doe
H101    Jack Doe
H102    John Smith
H102    Jane Smith
H102    Katie Smith
H102    Jack Smith

显然我想在更大范围内这样做。列数介于1和1之间。 6,所以我不能这样限制它。我能够得到一个脚本,允许我把每个人放在一行。但是,我很难将第一列复制到每一行。

Sub ToOneColumn()
Dim i As Long, k As Long, j As Integer
Application.ScreenUpdating = False
Columns(2).Insert
i = 0
k = 1
While Not IsEmpty(Cells(k, 3))
    j = 3
    While Not IsEmpty(Cells(k, j))
        i = i + 1
        Cells(i, 1) = Cells(k, 1) //CODE IN QUESTION
        Cells(i, 2) = Cells(k, j)
        Cells(k, j).Clear
        j = j + 1
    Wend
    k = k + 1
Wend
Application.ScreenUpdating = True
End Sub

就像我说的那样,将每个人都放在自己的行上工作得很好,但无法弄清楚如何获得第一列。它似乎应该如此简单,但这让我烦恼。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我认为你在第1列中写了你的数据。 您为名称创建了一个新列,但没有为第一列创建新列,但它完全取决于您的原始格式。

试试这个:

Sub ToOneColumn()
Dim i As Long, k As Long, j As Integer
Application.ScreenUpdating = False
Columns(2).Insert
Columns(2).Insert
i = 0
k = 1
While Not IsEmpty(Cells(k, 3))
    j = 4
    While Not IsEmpty(Cells(k, j))
        i = i + 1
        Cells(i, 2) = Cells(k, 1) //CODE IN QUESTION
        Cells(i, 3) = Cells(k, j)
        Cells(k, j).Clear
        j = j + 1
    Wend
    k = k + 1
Wend
Application.ScreenUpdating = True
End Sub

然后删除除了两个创建的列之外的所有列。