2011-02-08 27 views
-1

一排我有我需要拉一個數據源5列:
Line1|Line2|Line3|Line4|Line5將5列從一個表到另一個地方,但到

...所有在他們之下的數據。我需要將這5列添加到新工作表中,不僅要重命名它們,還要爲每條記錄創建更多列。

如:
shop1|add1|citystate1|phone1|web1|shop2|add2|citystate2|phone2|web2| etc.

...與數據的相應列下下降。每個記錄的列僅相同。

屏幕截圖

數據源圖像是什麼數據的模樣了。除了我將這些列複製出原來的外,因爲還有其他列。我只需要那5列。

http://dl.dropbox.com/0/view/vj1kgmzz6p44v4v/links/datasource.png

結果圖像是我需要它結束了。可能會有數百條記錄。標題需要按順序顯示。我只包括前幾列,但是這些列橫向延伸了幾條記錄。

http://dl.dropbox.com/0/view/gu7x05nqncphl0b/links/result.png

+0

SO上沒有私人消息功能,但您可以留下評論引導某人訪問該圖片。 –

回答

-1

Concatenate功能可能會做你想要什麼。

+0

我不認爲這是我想要的。正如我所說的,我對Excel不太瞭解。我看到的是Concatenate用於將細胞組合成一個細胞。我不想結合任何東西。我只是想將數據移動到一個新的工作表中,但將其放在一個長行中,並且標題不同且順序。 –

+0

然後你在尋找轉置嗎? –

0

moveShiftLaterally_before
的樣本數據

的聯繫信息的立式清單將被直接價值轉移是最方便處理。

Sub moveShiftLaterally_Values() 
    Dim strHDR As String, rw As Long, cls As Long, vHDRs As Variant 

    strHDR = "shop0|add0|citystate0|phone0|web0" 

    Worksheets("Sheet1").Copy After:=Worksheets("Sheet1") 
    ActiveSheet.Name = "horizList" 

    With Worksheets("horizList") 
     For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1 
      'assign the correct increment and split the header string 
      vHDRs = Split(Replace(strHDR, 0, rw - 1), Chr(124)) 
      'transfer the headers 
      .Cells(1, 1).Offset(0, (rw - 2) * (UBound(vHDRs) + 1)).Resize(1, UBound(vHDRs) + 1) = vHDRs 
      'transfer the values 
      .Cells(2, 1).Offset(0, (rw - 2) * (UBound(vHDRs) + 1)).Resize(1, UBound(vHDRs) + 1) = _ 
       .Cells(rw, 1).Resize(1, UBound(vHDRs) + 1).Value 
     Next rw 
     'remove the original entries 
     .Cells(1, 1).CurrentRegion.Offset(2, 0).Clear 
    End With 

End Sub 

moveShiftLaterally_Values_after
後moveShiftLaterally_Values

然而,隨着自定義數字可能性格式化的電話號碼和不同的是應水平勻漿列寬,加入Range.PasteSpecial method的某些XlPasteType面到第一晶種目的地細胞可能最終被證明是最好的方法。

Sub moveShiftLaterally_All() 
    Dim strHDR As String, rw As Long, cls As Long, vHDRs As Variant 

    strHDR = "shop0|add0|citystate0|phone0|web0" 

    Worksheets("Sheet1").Copy After:=Worksheets("Sheet1") 
    ActiveSheet.Name = "horizList" 

    With Worksheets("horizList") 
     'seed the cell formats and column widths first 
     With .Cells(1, 1).CurrentRegion 
      With .Resize(2, .Columns.Count) 
       .Copy 
       For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 3 Step -1 
        'transfer the column widths and cell formatting 
        .Cells(1, 1).Offset(0, (rw - 2) * .Columns.Count).PasteSpecial _ 
         Paste:=xlPasteColumnWidths 
        .Cells(1, 1).Offset(0, (rw - 2) * .Columns.Count).PasteSpecial _ 
         Paste:=xlPasteFormats 
       Next rw 
       Application.CutCopyMode = False 
      End With 
     End With 
     'transfer the HDR and VALs 
     For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1 
      'assign the correct increment and split the header string 
      vHDRs = Split(Replace(strHDR, 0, rw - 1), Chr(124)) 
      'transfer the headers 
      .Cells(1, 1).Offset(0, (rw - 2) * (UBound(vHDRs) + 1)).Resize(1, UBound(vHDRs) + 1) = vHDRs 
      'transfer the values 
      .Cells(2, 1).Offset(0, (rw - 2) * (UBound(vHDRs) + 1)).Resize(1, UBound(vHDRs) + 1) = _ 
       .Cells(rw, 1).Resize(1, UBound(vHDRs) + 1).Value 
     Next rw 
     'remove the original entries 
     .Cells(1, 1).CurrentRegion.Offset(2, 0).Clear 
    End With 

End Sub 

moveShiftLaterally_All_after.後moveShiftLaterally_Values

我將它留給你來決定哪種方法適合你的目的。

相關問題