2014-06-18 131 views
0

任何人都可以幫助或指導我如何在Excel中創建一個宏,該宏將在複製所有其他周圍值時從某些列創建新行?Excel VBA - 組合和重複行/列

例如,我需要把這個:

A 1 2 3 4 B 
C 5 6 7 8 D 

進入這個:

A 1 B 
A 2 B 
A 3 B 
A 4 B 
C 5 D 
C 6 D 
C 7 D 
C 8 D 

我已經找到一種方法使用下面的宏列組合在一起:

Sub SingleColumn() 
Dim CurSh As Worksheet, NewSh As Worksheet, Rng As Range, Col As Long 

Set CurSh = ActiveSheet 
Set NewSh = Sheets.Add 
CurSh.Activate 
Set Rng = Application.Intersect(Selection, CurSh.UsedRange) 

For Col = 1 To Selection.Columns.Count 
    Rng.Range(Cells(1, Col), Cells(Rng.Rows.Count, Col)).Copy NewSh.Range("a65536").End(xlUp).Offset(1, 0) 
Next Col 
End Sub 

回答

0

修改如下代碼:

Sub SingleColumn() 
    Dim CurSh As Worksheet, NewSh As Worksheet, Rng As Range, Col As Long, Row As Long 

    Set CurSh = ActiveSheet 
    Set NewSh = Sheets.Add 
    CurSh.Activate 
    Set Rng = Application.Intersect(Selection, CurSh.UsedRange) 

    For Row = 1 To Selection.Rows.Count 
     For Col = 1 To Selection.Columns.Count 
      Rng.Range(Cells(Row, Col).Address).Copy NewSh.Range("A1").Offset(Col - 1, Row - 1) 
     Next 
    Next 
End Sub 

對我來說,你有一點點複雜的宏...