基于引用第一列,将行转换为excel中的列

时间:2013-12-30 10:43:17

标签: excel excel-formula

我有一个包含以下信息的文件

#Name           Image           Image   Image
product 1       image111A.jpg   image111B.jpg   
product 2       image222A.jpg   image222B.jpg   image222C.jpg
product 3       image333A.jpg   image333B.jpg   

并希望使用宏或公式

在excel中转​​换为如下所示
#Name         Image
product 1     image111A.jpg
product 1     image111B.jpg
product 2     image222A.jpg
product 2     image222B.jpg
product 2     image222C.jpg
product 3     image333A.jpg
product 3     image333B.jpg

1 个答案:

答案 0 :(得分:0)

可能应该是以下内容。它可以做得更好,但我认为它会更难理解。

Sub MakeItFlat()
Dim sourceSheet, targetSheet As Worksheet
Dim counterRow, counterColumn, counterTarget As Integer
Dim tempString As String
Set sourceSheet = Application.ActiveWorkbook.Worksheets("Sheet1")
Set targetSheet = Application.ActiveWorkbook.Worksheets("Sheet2")

counterRow = 0
counterTarget = 0
Do While sourceSheet.Range("A1").Offset(counterRow, 0).Value <> ""
    tempString = sourceSheet.Range("A1").Offset(counterRow, 0).Value
    counterColumn = 1
    Do While sourceSheet.Range("A1").Offset(counterRow, counterColumn).Value <> ""
        targetSheet.Range("A1").Offset(counterTarget, 0).Value = tempString
        targetSheet.Range("A1").Offset(counterTarget, 1).Value = _ 
             sourceSheet.Range("A1").Offset(counterRow, counterColumn).Value
        counterTarget = counterTarget + 1
        counterColumn = counterColumn + 1
    Loop
    counterRow = counterRow + 1
Loop

End Sub