Excel VBA - 组合和复制行/列

时间:2014-06-18 00:58:46

标签: excel excel-vba vba

任何人都可以帮助或指导我如何在Excel中创建一个宏,该宏将从某些列创建新行,同时复制所有其他周围值?

例如,我需要转变一下:

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

1 个答案:

答案 0 :(得分: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

对我来说,你的宏有点复杂......